Possible Duplicates:
pimpl: shared_ptr or unique_ptr
smart pointers (boost) explained
Could someone explain differences between shared_ptr and unique_ptr?
This question is related to
c++
pointers
c++11
shared-ptr
unique-ptr
unique_ptr
is a smart pointer which owns an object exclusively.
shared_ptr
is a smart pointer for shared ownership. It is both copyable
and movable
. Multiple smart pointer instances can own the same resource. As soon as the last smart pointer owning the resource goes out of scope, the resource will be freed.
When wrapping a pointer in a unique_ptr
you cannot have multiple copies of unique_ptr
. The shared_ptr
holds a reference counter which count the number of copies of the stored pointer. Each time a shared_ptr
is copied, this counter is incremented. Each time a shared_ptr
is destructed, this counter is decremented. When this counter reaches 0, then the stored object is destroyed.
unique_ptr
is the light-weight smart pointer of choice if you just have a dynamic object somewhere for which one consumer has sole (hence "unique") responsibility -- maybe a wrapper class that needs to maintain some dynamically allocated object. unique_ptr
has very little overhead. It is not copyable, but movable. Its type is template <typename D, typename Deleter> class unique_ptr;
, so it depends on two template parameters.
unique_ptr
is also what auto_ptr
wanted to be in the old C++ but couldn't because of that language's limitations.
shared_ptr
on the other hand is a very different animal. The obvious difference is that you can have many consumers sharing responsibility for a dynamic object (hence "shared"), and the object will only be destroyed when all shared pointers have gone away. Additionally you can have observing weak pointers which will intelligently be informed if the shared pointer they're following has disappeared.
Internally, shared_ptr
has a lot more going on: There is a reference count, which is updated atomically to allow the use in concurrent code. Also, there's plenty of allocation going on, one for an internal bookkeeping "reference control block", and another (often) for the actual member object.
But there's another big difference: The shared pointers type is always template <typename T> class shared_ptr;
, and this is despite the fact that you can initialize it with custom deleters and with custom allocators. The deleter and allocator are tracked using type erasure and virtual function dispatch, which adds to the internal weight of the class, but has the enormous advantage that different sorts of shared pointers of type T
are all compatible, no matter the deletion and allocation details. Thus they truly express the concept of "shared responsibility for T
" without burdening the consumer with the details!
Both shared_ptr
and unique_ptr
are designed to be passed by value (with the obvious movability requirement for the unique pointer). Neither should make you worried about the overhead, since their power is truly astounding, but if you have a choice, prefer unique_ptr
, and only use shared_ptr
if you really need shared responsibility.
Source: Stackoverflow.com