assigning one shared_ptr to another

c++11, shared-ptr

Solution

Yes, `pA` does not point to the char `'A'` anymore, so the reference count is decremented. As it was the only reference to `'A'`, the reference count reaches zero and the char is deleted. It would be highly surprising and error-prone if you'd have to explicitly release the reference before reassignment.

`pA.reset(pB)` should not compile, as `reset` can only take a raw pointer, not another `shared_ptr`.

Problem

Does assigning one shared pointer to another free the memory managed by the latter? Let ``` typedef shared_ptr<char> char_ptr_t; char_ptr_t pA(new char('A')); char_ptr_t pB(new char('B')); ``` Now, does the below statement free the memory of `'A'`? ``` /*1*/ pA = pB; ``` Or do I need to explicitly free it: ``` /*2*/ pA.reset(); /*3*/ pA = pB; ``` And, is the following code valid for achieving the same? ``` /*4*/ pA.reset(pB); //<-- is this valid? Not compiling in MSVC++ 2010, though the standard seems to allow it. ```

Original source