HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Differences between unique_ptr and shared_ptr

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
andbetweendifferencesshared_ptrunique_ptr

Problem

Possible Duplicates:

pimpl: shared_ptr or unique_ptr

smart pointers (boost) explained

Could someone explain differences between shared_ptr and unique_ptr?

Solution

Both of these classes are smart pointers, which means that they automatically (in most cases) will deallocate the object that they point at when that object can no longer be referenced. The difference between the two is how many different pointers of each type can refer to a resource.

When using unique_ptr, there can be at most one unique_ptr pointing at any one resource. When that unique_ptr is destroyed, the resource is automatically reclaimed. Because there can only be one unique_ptr to any resource, any attempt to make a copy of a unique_ptr will cause a compile-time error. For example, this code is illegal:

unique_ptr myPtr(new T);       // Okay
unique_ptr myOtherPtr = myPtr; // Error: Can't copy unique_ptr


However, unique_ptr can be moved using the new move semantics:

unique_ptr myPtr(new T);                  // Okay
unique_ptr myOtherPtr = std::move(myPtr); // Okay, resource now stored in myOtherPtr


Similarly, you can do something like this:

unique_ptr MyFunction() {
    unique_ptr myPtr(/* ... */);

    /* ... */

    return myPtr;
}


This idiom means "I'm returning a managed resource to you. If you don't explicitly capture the return value, then the resource will be cleaned up. If you do, then you now have exclusive ownership of that resource." In this way, you can think of unique_ptr as a safer, better replacement for auto_ptr.

shared_ptr, on the other hand, allows for multiple pointers to point at a given resource. When the very last shared_ptr to a resource is destroyed, the resource will be deallocated. For example, this code is perfectly legal:

shared_ptr myPtr(new T);       // Okay
shared_ptr myOtherPtr = myPtr; // Sure!  Now have two pointers to the resource.


Internally, shared_ptr uses reference counting to track how many pointers refer to a resource, so you need to be careful not to introduce any reference cycles.

In short:

  • Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed.



  • Use shared_ptr when you want multiple pointers to the same resource.

Code Snippets

unique_ptr<T> myPtr(new T);       // Okay
unique_ptr<T> myOtherPtr = myPtr; // Error: Can't copy unique_ptr
unique_ptr<T> myPtr(new T);                  // Okay
unique_ptr<T> myOtherPtr = std::move(myPtr); // Okay, resource now stored in myOtherPtr
unique_ptr<T> MyFunction() {
    unique_ptr<T> myPtr(/* ... */);

    /* ... */

    return myPtr;
}
shared_ptr<T> myPtr(new T);       // Okay
shared_ptr<T> myOtherPtr = myPtr; // Sure!  Now have two pointers to the resource.

Context

Stack Overflow Q#6876751, score: 702

Revisions (0)

No revisions yet.