-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemAllocateTest.cpp
More file actions
96 lines (79 loc) · 2.22 KB
/
MemAllocateTest.cpp
File metadata and controls
96 lines (79 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// MemAllocateTest [C++14]
#include <iostream>
#include <vector>
constexpr std::size_t kb = 1024;
constexpr std::size_t mb = 1024 * 1024;
constexpr std::size_t gb = 1024 * 1024 * 1024;
constexpr std::size_t reservesize{ gb * 16 };
void showsize(const std::size_t size) {
std::cout << "[ "
<< size / gb << " GB | "
<< size % gb / mb << " MB | "
<< size % mb / kb << " KB | "
<< size % kb << " B ]"
<< std::endl;
}
void __fastcall func(bool flag = false) {
std::vector<std::uint8_t> vec;
if(flag) vec.reserve(reservesize);
std::size_t vecCapacity{ 0 };
std::size_t i = 0;
try {
while (i < reservesize) {
vec.push_back(i % 256);
if (vec.capacity() != vecCapacity) {
vecCapacity = vec.capacity();
std::cout << "(vector re-located) new capacity: " << vecCapacity << std::endl;
}
++i;
}
}
catch (const std::bad_alloc& e) {
std::cout << "(vector re-located): " << e.what() << std::endl;
}
showsize(i);
std::cout << "Press ENTER to end this step..." << std::endl;
std::cin.ignore();
}
int main() {
std::cout << "Press ENTER to run step ONE..." << std::endl;
std::cin.ignore();
func(false);
std::cout << "Press ENTER to run step TWO..." << std::endl;
std::cin.ignore();
func(true);
std::cout << "Press ENTER to run step THREE..." << std::endl;
std::cin.ignore();
std::vector<std::uint8_t> vec;
std::size_t vecCapacity{ 0 };
std::size_t i = 0;
try {
while (i < reservesize) {
vec.push_back(i % 256);
if (vec.capacity() != vecCapacity) {
vecCapacity = vec.capacity();
std::cout << "(vector re-located) new capacity: " << vecCapacity << std::endl;
}
++i;
}
}
catch (const std::bad_alloc& e) {
std::cout << "(vector re-located): " << e.what() << std::endl;
}
showsize(vec.capacity());
std::cout << "Press ENTER to use clear()..." << std::endl;
std::cin.ignore();
vec.clear();
showsize(vec.capacity());
std::cout << "Press ENTER to use resize(4096)..." << std::endl;
std::cin.ignore();
vec.resize(4096);
showsize(vec.capacity());
std::cout << "Press ENTER to use shrink_to_fit()..." << std::endl;
std::cin.ignore();
vec.shrink_to_fit();
showsize(vec.capacity());
std::cout << "Press ENTER to close programm..." << std::endl;
std::cin.ignore();
return 0;
}