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; } |
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.