In this article we will discuss a Smart Pointer implementationstd::unique_ptr<> provided by c++11.

what is std::unique_ptr ?

unique_ptr<> is one of the Smart pointer implementation provided by c++11 to prevent memory leaks. A unique_ptr object wraps around a raw pointer and its responsible for its lifetime. When this object is destructed then in its destructor it deletes the associated raw pointer.

unique_ptr has its -> and * operator overloaded, so it can be used similar to normal pointer.

Checkout the following example,

Output:

unique_ptr<Task> object taskPtr accepts a raw pointer as arguments. Now when function will exit, this object will go out of scope and its destructor will be called. In its destructor unique_ptr object taskPtr deletes the associated raw pointer.

So, even if function is exited normally or abnormally (due to some exception), destructor of taskPtr will always be called. Hence, raw pointer will always get deleted and prevent the memory leak.

Unique Ownership of unique pointer

A unique_ptr object is always the unique owner of associated raw pointer. We can not copy a unique_ptr object, its only movable.

As each unique_ptr object is sole owner of a raw pointer, therefore in its destructor it directly deletes the associated pointer. There is no need of any reference counting, therefore its very light.

Creating a empty unique_ptr object

Let’s create a empty unique_ptr<int> object i.e.

ptr1 has no raw pointer associated with it. Hence its empty.

Check if a unique_ptr<> object is empty

There are two ways to check if a unique_ptr<> object is empty or it has a raw pointer associated with it i.e.

Method 1 :

Method 2:

Creating a unique_ptr object with raw pointer

To create a unique_ptr<> object that is non empty, we need to pass the raw pointer in its constructor while creating the object i.e.

We can not create a unique_ptr<> object through assignment, otherwise it will cause compile error

Reseting a unique_ptr

Calling reset() function on a unique_ptr<> object will reset it i.e. it will delete the associated raw pointer and make unique_ptr<> object empty i.e.

unique_ptr object is not copyable

As unique_ptr<> is not copyable, only movable. Hence we can not create copy of a unique_ptr object either through copy constructor or assignment operator.

Both copy constructor and assignment operator are deleted in unique_ptr<> class.

Transfering the ownership of unique_ptr object

We cannot copy a unique_ptr object, but we can move them. It means a unique_ptr object can transfer the owner ship of associated raw pointer to another unique_ptr object. Let’s understand by an example,

Create a unique_ptr Object i.e.

taskPtr2 is not empty.

Now transfer the ownership of associated pointer of Task to a new unique_ptr object i.e.

std::move() will convert the taskPtr2 to a RValue Reference. So that move constructor of unique_ptr is invoked and associated raw pointer can be transferred to taskPtr4.

taskPtr2 will be empty after transferring the ownership of its raw pointer to taskPtr4.

Releasing the associated raw pointer

Calling release() on unique_ptr object will release the ownership of associated raw pointer from the object.
It returns the raw pointer.

Checkout complete example as follows,

Output:

 

 

 

 

Click Here to Subscribe for more Articles / Tutorials like this.