Constructors and Destructors

An exercise that Stroustrup wants the reader to do is understand the output of the following program.
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
#include "../../std_lib_facilities.h"

struct X {
	int val;

	void out(const string& s, int nv) {
	   cerr << this << "->" << s << ":" << val << "(" << nv << ")\n";
	}

	X() {
	   out("X()", 0);
	   val = 0;
	}

	X(int v) {
	   out("X(int)", v);
	   val = v;
	}

	X(const X& x) {
	   out("X(X&)", x.val);
	   val = x.val;
	}

	X& operator = (const X&a) {
	   out("X::operator = ()", a.val);
	   val = a.val;
	   return *this;
	}

	~X() {
	   out("~X()", 0);
	}
};

X glob(2);
X copy(X a) {return a;}
X copy2(X a) {
   X aa = a;
   return aa;
}

X& ref_to(X& a) {return a;}

X* make(int i) {
   X a(i);
   return new X(a);
}

struct XX {
   X a;
   X b;
};

int main ()
{
   X loc(4);
   X loc2 = loc;
   loc = X(5);
   loc2 = copy(loc);
   loc2 = copy2(loc);

   X loc3(6);
   X& r = ref_to(loc);
   delete make(7);
   delete make(8);
   vector<X> v(4);

   XX loc4;
   X* p = new X(9);
   delete p;

   X* pp = new X[5];
   delete[] pp;

   keep_window_open();
   return 0;
}


I don't understand this program. It is supposed to have different types of constructors and destructors, variables created and variables destroyed. It prints out whenever a variable is created or destroyed. I don't understand the output. It is a series of memory addresses with an X or an ~X after it. I understand the allocation of memory and the freeing of memory. I assume the net use of memory is zero, representing no memory leak. I can't follow what is going on. I can't cut and paste the output because it is in the console window, but here is a bit of it.

004241A0->X(int):0(2)
0012FF54->X(int):-858993460(4)
0012FF48->X(X&):-858993460(4)
0012FF54->X::operator = ():5(4)
0012FD30->X(X&):969884377(5)
0012FD30->~X():(5)

What does all this mean? Are there tutorials that explain this? Stroustrup has sort of thrown us in the water and left us on our own to find a way to swim.
To copy the output from the console window, right click on the console choose mark, drag it ovr the output, and press enter. then it will be copied to clip board, then paste it :P

Im not sure whats going on..I do know ~ is part of a destructor.

In school the way we learn code is much..cleaner than that so its kinda hard to read lol, using single letters as variables is something I never do unless in a for loop
@Drol,

Thank you for explaining how to copy and paste from the console window. I feel I need to point out, because it appears it wasn't clear in my original post. The program I posted was written by Bjarne Stroustrup and is designed to elicit a specific output that the reader is supposed to try to understand. HE decided to use single letters for the variable names. HE decided the upon format of the program.

The entire output is below.
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
004241A0->X(int):0(2)
0012FF54->X(int):-858993460(4)
0012FF48->X(X&):-858993460(4)
0012FD54->X(int):-858993460(5)
0012FF54->X::operator = ():4(5)
0012FD54->~X():5(0)
0012FD30->X(X&):702684171(5)
0012FD6C->X(X&):-858993460(5)
0012FD30->~X():5(0)
0012FF48->X::operator = ():4(5)
0012FD6C->~X():5(0)
0012FD30->X(X&):702684171(5)
0012FD10->X(X&):-858993460(5)
0012FD84->X(X&):-858993460(5)
0012FD10->~X():5(0)
0012FD30->~X():5(0)
0012FF48->X::operator = ():5(5)
0012FD84->~X():5(0)
0012FF3C->X(int):-858993460(6)
0012FD14->X(int):-858993460(7)
00345A48->X(X&):-842150451(7)
0012FD14->~X():7(0)
00345A48->~X():7(0)
0012FD14->X(int):-858993460(8)
00345A48->X(X&):-842150451(8)
0012FD14->~X():8(0)
00345A48->~X():8(0)
0012F7D8->X():-858993460(0)
00345AE0->X(X&):-842150451(0)
0012F7D8->~X():0(0)
0012F7D8->X():0(0)
00345AE4->X(X&):-842150451(0)
0012F7D8->~X():0(0)
0012F7D8->X():0(0)
00345AE8->X(X&):-842150451(0)
0012F7D8->~X():0(0)
0012F7D8->X():0(0)
00345AEC->X(X&):-842150451(0)
0012F7D8->~X():0(0)
0012FF04->X():-858993460(0)
0012FF08->X():-858993460(0)
00345B30->X(int):-842150451(9)
00345B30->~X():9(0)
00345B34->X():-842150451(0)
00345B38->X():-842150451(0)
00345B3C->X():-842150451(0)
00345B40->X():-842150451(0)
00345B44->X():-842150451(0)
00345B44->~X():0(0)
00345B40->~X():0(0)
00345B3C->~X():0(0)
00345B38->~X():0(0)
00345B34->~X():0(0)


I have thought more about the program and believe that on line 7: 'this' refers to the memory address, 's' refers to the name and type of the variable, 'val' refers to the value of the variable, I don't know what 'nv' refers to, perhaps array size.

So the when the global variable X glob(2) is created on line 36 it constructs a variable of size 2 at memory address 004241A0, name X and type int, of value 0. The address suggests to me that this variable was put in stack memory. Because it is global, it is never destroyed. That address is never written again.

I think line 37 copies 'glob(2)' to 'a', and 'a' is put into memory address 0012FF54, a heap memory address. It again has name X and is of type int. Line 38 copies 'a' to 'aa' and puts into memory address 0012FF48, again heap memory and the address is declining as heap addresses should (I think).

Am I in the ball park or way, way off?
Topic archived. No new replies allowed.