int *q = new int(42), *r = new int(100);
r = q;
r
and q
points to the same memory, while the old memory pointed by r
is leaked, since no pointer points to it and it is not freed.
auto q2 = make_shared<int>(42), r2 = make_shared<int>(100);
r2 = q2;
First, the reference count of r2
is decreased, and the memory pointed by r2
is freed, since no shared_ptr
points to it. Second, r2
and q2
point to the same memory, and the reference count of both smart pointers are increased.