unique_ptr in C++ (original) (raw)

Last Updated : 23 Jul, 2025

In C++, **unique_ptr is a smart pointer that manages a dynamically allocated object, and it was introduced in C++11. It is defined in the ****** header file. Here are the key points about unique_ptr:

Syntax of unique_ptr

C++ `

unique_ptr ptr1 (new A)

`

When we write unique_ptr ptr1 (new A), memory is allocated on the heap for an instance of datatype A. ptr1 is initialized and points to newly created A object. Here, ptr1 is the only owner of the newly created object A and it manages this object's lifetime. This means that when ptr1 is reset or goes out of scope, memory is automatically deallocated, and A's object is destroyed.

Since C++14, we can also create a unique pointer using make_unique() function.

C++ `

unique_ptr ptr1 = make_unique();

`

Examples of unique_ptr

Create a Unique Ptr

Lets create a structure A and it will have a method named printA to display some text. Then in the main section, let's create a unique pointer that will point to the structure A. So at this point, we have an instance of structure A and p1 holds the pointer to that.

C++ `

#include #include using namespace std;

struct A { void printA() { cout << "A struct...." << endl; } };

int main() {

unique_ptr<A> p1(new A);
p1->printA();

// displays address of the 
// containing pointer
cout << p1.get();
return 0;

}

`

Output

A struct.... 0x3e9dec20

Trying to Copy Object Owned by unique_ptr

C++ `

#include #include using namespace std;

struct A { void printA() { cout << "A struct...." << endl; } };

int main() {

unique_ptr<A> p1(new A);
p1->printA();

// displays address of the 
// containing pointer
cout << p1.get() << endl;

// will give compile time error
unique_ptr<A> p2 = p1;
p2->printA();
return 0;

}

`

**Output

main.cpp: In function ‘int main()’:
main.cpp🔞24: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&)
[with _Tp = A; _Dp = std::default_delete]’
18 | unique_ptr p2 = p1;
| ^~
In file included from /usr/include/c++/11/memory:76,
from main.cpp:3:
/usr/include/c++/11/bits/unique_ptr.h:468:7: note: declared here
468 | unique_ptr(const unique_ptr&) = delete;
| ^~~~~~~~~~

The above code will give compile time error as we cannot assign pointer p2 to p1 in case of unique pointers. We have to use the move semantics for such purpose as shown below.

Moving Object Owned by unique_ptr

C++ `

#include #include using namespace std;

struct A { void printA() { cout << "A struct...." << endl; } };

int main() {

unique_ptr<A> p1(new A);
p1->printA();

// displays address of the 
// containing pointer
cout << p1.get() << endl;

// now address stored in p1 
// shpould get copied to p2
unique_ptr<A> p2 = move(p1);

p2->printA();
cout << p1.get() << endl;
cout << p2.get();
return 0;

}

`

Output

A struct.... 0x3514c20 A struct.... 0 0x3514c20

In the above code, once the address in pointer p1 is copied to pointer p2, the pointer p1's address becomes NULL(0) and the address stored by p2 is now the same as the address stored by p1 showing that the address in p1 has been transferred to the pointer p2 using the move semantics.

When to use unique_ptr?

When ownership of resource is required. When we want single or exclusive ownership of a resource, then we should go for unique pointers. Only one unique pointer can point to one resource. So, one unique pointer cannot be copied to another. Also, it facilitates automatic cleanup when dynamically allocated objects go out of scope and helps preventing memory leaks.