-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmove.cc
170 lines (132 loc) · 3.48 KB
/
move.cc
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <memory>
#include <iostream>
#include <vector>
#include <string.h>
#include <string>
using namespace std;
void function()
{
auto uptr = std::make_unique<uint32_t>(42);
cout << *uptr << endl;
std::unique_ptr<uint32_t> uptr2 = std::unique_ptr<uint32_t>(new uint32_t(42));
// uptr2 = uptr;
cout << (void*)uptr.get() << endl; // prints address
cout << (void*)*reinterpret_cast<uint32_t**>(&uptr) << endl;
uptr2 = std::move(uptr);
cout << (void*)uptr.get() << endl; // prints 0
cout << (void*)uptr2.get() << endl; // prints same address
std::unique_ptr<int> iptr;
auto p = [](const auto& a) {
cout << "pointer is " << (a ? "" : "not ") << "set\n";
};
p(iptr);
cout << (void*) iptr.get() << endl; // prints 0
iptr = std::make_unique<int>(12);
p(iptr);
iptr.reset();
p(iptr);
}
std::string stringerror()
{
return strerror(errno);
}
struct SmartFP
{
SmartFP(const char* fname, const char* mode)
{
d_fp = fopen(fname, mode);
if(!d_fp)
throw std::runtime_error("Can't open file: " + stringerror());
}
SmartFP(SmartFP&& src)
{
d_fp = src.d_fp;
src.d_fp = 0;
}
~SmartFP()
{
if(d_fp)
fclose(d_fp);
}
FILE* d_fp{0};
};
SmartFP getTmpFP()
{
return SmartFP("/tmp/tmp", "w");
}
int main(int argc, char **argv)
{
SmartFP sfp("/etc/passwd", "r");
SmartFP sfp2(std::move(sfp));
vector<SmartFP> vec;
vec.emplace_back("move.cc", "r");
SmartFP sfp3 = getTmpFP();
function();
std::unique_ptr<uint32_t> testUnique;
std::shared_ptr<uint32_t> testShared;
uint32_t* testRaw;
cout<<"sizeof(testUnique): "<<sizeof(testUnique)<<endl;
cout<<"sizeof(testRaw): "<<sizeof(testRaw)<<endl;
cout<<"sizeof(testShared): "<<sizeof(testShared)<<endl;
if(argc < 2) {
cerr<<"Syntax: move u|s|ms|r|n"<<endl;
cerr<<"To test unique_ptr, shared_ptr, std::make_shared, raw and no pointers"<<endl;
exit(1);
}
if(*argv[1]=='u') {
vector<std::unique_ptr<uint32_t>> vec;
vec.reserve(10000000);
for(unsigned int n = 0 ; n < 10000000; ++n)
vec.emplace_back(std::make_unique<uint32_t>(n));
double val=0;
for(const auto& e : vec)
val += *e;
cout << val / vec.size() << endl;
}
else if(*argv[1]=='s') {
vector<std::shared_ptr<uint32_t>> vec;
vec.reserve(10000000);
for(unsigned int n = 0 ; n < 10000000; ++n) {
vec.emplace_back(std::shared_ptr<uint32_t>(new uint32_t(n)));
}
double val=0;
for(const auto& e : vec)
val += *e;
cout << val / vec.size() << endl;
}
else if(string(argv[1])=="ms") {
vector<std::shared_ptr<uint32_t>> vec;
vec.reserve(10000000);
for(unsigned int n = 0 ; n < 10000000; ++n) {
vec.emplace_back(std::make_shared<uint32_t>(n));
}
double val=0;
for(const auto& e : vec)
val += *e;
cout << val / vec.size() << endl;
}
else if(*argv[1]=='r') {
vector<uint32_t*> vec;
vec.reserve(10000000);
for(unsigned int n = 0 ; n < 10000000; ++n)
vec.emplace_back(new uint32_t(n));
double val=0;
for(const auto& e : vec)
val += *e;
cout << val / vec.size() << endl;
for(auto& e : vec)
delete e;
}
else if(*argv[1]=='n') {
vector<uint32_t> vec;
vec.reserve(10000000);
for(unsigned int n = 0 ; n < 10000000; ++n)
vec.emplace_back(n);
double val=0;
for(const auto& e : vec)
val += e;
cout << val / vec.size() << endl;
}
else
cerr<<"Unknown mode "<<argv[1]<<endl;
}