unique_ptr

std::unique_ptr was developed in C++11 as a replacement for std::auto_ptr. unique_ptr is a new facility with similar functionality, but with improved security (no fake copy assignments), added features (deleters), and support for arrays. It is a container for raw pointers. It explicitly prevents copying of its contained pointer as would happen with a normal assignment i.e. it allows exactly one owner of the underlying pointer. So, when using unique_ptr there can only be at most one unique_ptr at any one resource and when that unique_ptr is destroyed, the resource is automatically claimed. Also, since there can only be one unique_ptr to any resource, any attempt to make a copy of unique_ptr will cause a compile-time error.

 unique_ptr<A> ptr1 (new A);

 // Error: can't copy unique_ptr
 unique_ptr<A> ptr2 = ptr1;    

But, unique_ptr can be moved using the new move semantics i.e. using the std::move() function to transfer ownership of the contained pointer to another unique_ptr.

// Works, resource now stored in ptr2
unique_ptr<A> ptr2 = move(ptr1); 

So, it’s best to use unique_ptr when we want a single pointer to an object that will be reclaimed when that single pointer is destroyed. 

Example:

C++




// C++ program to illustrate the use of unique_ptr
#include <iostream>
#include <memory>
using namespace std;
  
class A {
public:
    void show() { cout << "A::show()" << endl; }
};
  
int main()
{
    unique_ptr<A> p1(new A);
    p1->show();
  
    // returns the memory address of p1
    cout << p1.get() << endl;
  
    // transfers ownership to p2
    unique_ptr<A> p2 = move(p1);
    p2->show();
    cout << p1.get() << endl;
    cout << p2.get() << endl;
  
    // transfers ownership to p3
    unique_ptr<A> p3 = move(p2);
    p3->show();
    cout << p1.get() << endl;
    cout << p2.get() << endl;
    cout << p3.get() << endl;
  
    return 0;
}


Output

A::show()
0xab4010
A::show()
0
0xab4010
A::show()
0
0
0xab4010

The below code returns a resource and if we don’t explicitly capture the return value, the resource will be cleaned up. If we do, then we have exclusive ownership of that resource. In this way, we can think of unique_ptr as a safer and better replacement for auto_ptr.

unique_ptr<A> fun()
{
    unique_ptr<A> ptr(new A);

    /* ...
       ... */

    return ptr;
}

auto_ptr vs unique_ptr vs shared_ptr vs weak_ptr in C++

Prerequisite Smart Pointers

Smart Pointer is a  pointer-wrapping stack-allocated object. Smart pointers, in plain terms, are classes that wrap a pointer, or scoped pointers.

C++ libraries provide implementations of smart pointers in the following types:

  • auto_ptr
  • unique_ptr
  • shared_ptr
  • weak_ptr

They all are declared in a memory header file( #include<memory>) which is used to manage dynamic memory allocation.  

Similar Reads

auto_ptr

This class template is deprecated as of C++11. unique_ptr is a new facility with similar functionality, but with improved security. auto_ptr is a smart pointer that manages an object obtained via a new expression and deletes that object when auto_ptr itself is destroyed. An object when described using the auto_ptr class it stores a pointer to a single allocated object which ensures that when it goes out of scope, the object it points to must get automatically destroyed. It is based on an exclusive ownership model i.e. two pointers of the same type can’t point to the same resource at the same time. As shown in the below program, copying or assigning pointers changes the ownership i.e. source pointer has to give ownership to the destination pointer....

unique_ptr

...

When to use unique_ptr?

std::unique_ptr was developed in C++11 as a replacement for std::auto_ptr. unique_ptr is a new facility with similar functionality, but with improved security (no fake copy assignments), added features (deleters), and support for arrays. It is a container for raw pointers. It explicitly prevents copying of its contained pointer as would happen with a normal assignment i.e. it allows exactly one owner of the underlying pointer. So, when using unique_ptr there can only be at most one unique_ptr at any one resource and when that unique_ptr is destroyed, the resource is automatically claimed. Also, since there can only be one unique_ptr to any resource, any attempt to make a copy of unique_ptr will cause a compile-time error....

shared_ptr

...

When to use shared_ptr?

Use unique_ptr when you want to have single ownership(Exclusive) of the resource. Only one unique_ptr can point to one resource. Since there can be one unique_ptr for a single resource it’s not possible to copy one unique_ptr to another....

weak_ptr

A shared_ptr is a container for raw pointers. It is a reference counting ownership model i.e. it maintains the reference count of its contained pointer in cooperation with all copies of the shared_ptr. So, the counter is incremented each time a new pointer points to the resource and decremented when the destructor of the object is called....

When to use weak_ptr?

...