-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsizeof6.cpp
58 lines (44 loc) · 1.2 KB
/
sizeof6.cpp
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
#include <iostream>
using namespace std;
#include <math.h>
#define SIZE 10000000
class Pt {
public:
// Pt (int x, int y) {_x = x; _y = y; }
Pt (int x, int y) : _x(x), _y(y) { _z = sqrt(x*x + y*y); big_buf = new float [10000]; } // ctor (constructor) 1
Pt (int x, int y, int z) : _x(x), _y(y), _z(z) { big_buf = new float [10000]; } // ctor (constructor) 2
Pt () { big_buf = new float [10000]; }
~Pt() { if(big_buf) delete [] big_buf; big_buf = (float*) 0; } // dtor (destructor)
int getX( ) { clearBigBuf(); return _x; }
int getY( ) { return _y; }
float getZ( ) { return _z; }
private:
int _x;
int _y;
float _z;
float *big_buf;
void clearBigBuf() { for (int i = 0; i < 10000; i++) big_buf[i]= 27.92; }
};
int main(int argc, char **)
{
Pt *big_buf;
Pt p1;
Pt p2;
Pt p3;
Pt p4(123,456);
// p3.clearBigBuf(); // private func, cannot call.
cout << "x,y,z = " << p4.getX() << "," << p4.getY() << "," << p4.getZ() << endl;
#if 0
cout << sizeof(big_buf) << endl;
for(int i = 1; 1; i++) {
cout << i << ": allocating " << SIZE << " bytes\n";
big_buf = new Pt [SIZE];
cout << big_buf << endl;
if(!big_buf) {
cout << "new failed!\n";
break;
}
delete [] big_buf;
}
#endif
}