One of its weakest use-cases is in containers. This is because if a copy of an auto_ptr<> is made and the old copy is not carefully reset, then both copies of the auto pointer will attempt to delete the object, resulting in a double-free incontainer may delete the worst casepointer and lose data.
One of its weakest use-cases is in containers. This is because if a copy of an auto_ptr<> is made and the old copy is not carefully reset, then both copies of the auto pointer will attempt to delete the object, resulting in a double-free in the worst case.
One of its weakest use-cases is in containers. This is because if a copy of an auto_ptr<> is made and the old copy is not carefully reset, then the container may delete the pointer and lose data.
Now, if we remove a Node, there's a tanglingcyclic reference to it. It'll never be deleted because its reference count will never be zero.
Now, things will work correctly, and removing a node will not leave danglingstuck references to the parent node. It makes walking the tree a little more complicated, however:
If thrower() throws an exception, both lines will leak memory. And more than that, shared_ptr<> holds the pointerreference count far away from the object it points to and this cancan mean a second allocation). That's not usually desirable.
Now, in both cases, even if thrower() throws an exception, there will not be a leak of memory. As a bonus, make_shared<>() has the opportunity to create a managed pointerits reference count in the same memory space as its managermanaged object, which can both be faster and can save a few bytes of memory, while giving you an exception safety guarantee!
Now, if we remove a Node, there's a tangling reference to it. It'll never be deleted because its reference count will never be zero.
Now, things will work correctly, and removing a node will not leave dangling references to the parent node. It makes walking the tree a little more complicated, however:
If thrower() throws an exception, both lines will leak memory. And more than that, shared_ptr<> holds the pointer far away from the object it points to and this can mean a second allocation). That's not usually desirable.
Now, in both cases, even if thrower() throws an exception, there will not be a leak of memory. As a bonus, make_shared<>() has the opportunity to create a managed pointer in the same memory space as its manager object, which can both be faster and can save a few bytes of memory, while giving you an exception safety guarantee!
Now, if we remove a Node, there's a cyclic reference to it. It'll never be deleted because its reference count will never be zero.
Now, things will work correctly, and removing a node will not leave stuck references to the parent node. It makes walking the tree a little more complicated, however:
If thrower() throws an exception, both lines will leak memory. And more than that, shared_ptr<> holds the reference count far away from the object it points to and this can mean a second allocation). That's not usually desirable.
Now, in both cases, even if thrower() throws an exception, there will not be a leak of memory. As a bonus, make_shared<>() has the opportunity to create its reference count in the same memory space as its managed object, which can both be faster and can save a few bytes of memory, while giving you an exception safety guarantee!
If thrower() throws an exception, both lines will leak memory. And more than that, both unique_ptr<> and shared_ptr<> make a separate allocation to holdholds the pointer far away from the object it points to and this can mean a second allocation). That's not usually desirable.
Now, in both cases, even if thrower() throws an exception, there will not be a leak of memory. As a bonus, make_unique<>() and make_shared<>() both havehas the opportunity to create a managed pointer in the same memory space as the allocatedits manager object, which can both be faster and can save a few bytes of memory, while giving you an exception safety guarantee!
If thrower() throws an exception, both lines will leak memory. And more than that, both unique_ptr<> and shared_ptr<> make a separate allocation to hold the pointer. That's not usually desirable.
Now, in both cases, even if thrower() throws an exception, there will not be a leak of memory. As a bonus, make_unique<>() and make_shared<>() both have the opportunity to create a managed pointer in the same memory space as the allocated object, which can both be faster and can save a few bytes of memory, while giving you an exception safety guarantee!
If thrower() throws an exception, both lines will leak memory. And more than that, shared_ptr<> holds the pointer far away from the object it points to and this can mean a second allocation). That's not usually desirable.
Now, in both cases, even if thrower() throws an exception, there will not be a leak of memory. As a bonus, make_shared<>() has the opportunity to create a managed pointer in the same memory space as its manager object, which can both be faster and can save a few bytes of memory, while giving you an exception safety guarantee!