-
Notifications
You must be signed in to change notification settings - Fork 33
/
12.09.txt
29 lines (19 loc) · 863 Bytes
/
12.09.txt
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
Exercise 12.9: Explain what happens in the following code:
int *q = new int(42), *r = new int(100);
r = q;
auto q2 = make_shared<int>(42), r2 = make_shared<int>(100);
r2 = q2;
By Faisal Saadatmand
int *q = new int(42), *r = new int(100);
q and r are pointers to int pointing to memory blocks on the heap,
initialized with values 42 and 100 respectively.
r = q;
r points at what q points: value 100. r's old object is "lost", but its memory
is not freed.
auto q2 = make_shared<int>(42), r2 = make_shared<int>(100);
q2 and r2 are shared smart pointers to int pointing to memory blocks on the
heap, initialized with values 42 and 100 respectively.
r2 = q2;
r2 points to what q2 points to. r2's reference counter is decrement. It is now
zero, and therefore, its old object is destroyed and its memory is freed. q's
reference counter is incremented.