The pointer which automatically deletes itself known as a Smart pointer. Smart pointers are objects that act as a wrapper around the raw pointer. Smart pointers only point to heap allocated memory and automatically call delete when pointers are no longer needed.

Why we should use smart pointers?

On using standard raw pointers, we use memory in heap. If there are a large number of pointers in our program then definitely we will need a large amount of heap memory. Apart from that, we as a programmer create overhead for ourselves to properly free up space at all those points wherever the use of the pointer is over.

If we do not do that in a proper manner then we may end up creating the memory leak.

So by using smart pointers instead of raw pointers, we do not have to worry about deleting pointer or memory leak.

Types of Smart Pointers

There are 3 types of smart pointers:

  1. Unique Pointer (unique_ptr)
  2. Shared Pointer (smart_ptr)
  3. Weak Pointer (weak_ptr)

Note: Auto pointer (auto_ptr) has been depreciated. And to use smart pointers memory header file must be included as it contains the class and member methods used by the smart pointers.

Unique Pointer

Unique pointer is simple smart pointer which is very efficient to use.

Syntax to declare the unique pointer

where T is the template parameter which is the type of the object that the smart pointer will manage in the heap. Hence unique_ptr can point to any type we need.

Properties of Unique Pointer

  • Points to any object of type T on the heap.
  • There can be only one unique_ptr<T> pointing to a single object on the heap. Hence no other pointer can point to this object (i.e Owns what it points to).
  • Cannot be assigned or copied.
  • Can be moved.
  • When pointer is destroyed, what it points to automatically get destroyed.

Initialization of Unique Pointer

Note: make_unique<T> is a function available since C++14. It return unique pointer of a specified type and allows you to pass initialization value into the constructor of the managed object.

Unique Pointer in C++ Example


Smart pointer in C++

In the Output, notice that when we use raw object pointer the destructor of the class hasn’t been called, whereas in case of smart object pointer, the destructor is invoked automatically as soon as the control goes out of the scope (i.e Object is destroyed automatically), hence preventing memory leak.

Shared Pointer

Like Unique Pointer, Shared Pointer points to an object in the heap memory, however unlike that unique pointer the shared pointer can be shared among other shared pointer.

Syntax to Declare Shared Pointer

where T is the template parameter which is the type of the object that the smart pointer will manage in the heap.

Properties of Shared Pointer

  • Points to an object of Type T on the heap memory.
  • There can be many shared pointers pointing to the same object on the heap ( There is shared ownership relationship ).
  • Can be assigned, copied and moved.

Each shared pointer object has a count number associated with it, which can be accessed with use_count() method.

use_count() method returns the number of shared pointer objects managing the heap object.

Initialization of Shared Pointer

Shared Pointer C++ Example


Unique, shared and weak smart Pointers in C++


  • Primitive Smart Pointer Output Part: The count value increased to 2, when one pointer is shared with the other pointer and it decreases back to 1, when the second shared pointer is destroyed outside the scope.
  •  Object Pointer Part: Destructor is not called in case of raw pointer but called automatically by smart pointer when it gets destroyed.

Weak Pointer

Like Shared Pointer, Weak Pointer points to an object in the heap memory, however unlike shared pointer, weak pointer doesn’t participate in the owning relationship.

Syntax to Declare Weak Pointer

Properties of Weak Pointer

  • Points to an object of type T on the heap.
  • Doesn’t participate in owning relationship.
  • Always created from a shared_ptr.
  • Does not increment or decrement reference use count.
  • Used to prevent strong reference cycles, which could prevent objects from being deleted.

That’s all for smart pointers in C++, next we will discuss deep about strong reference cycles and custom deleters in C++, till then if you have any doubt then comment below.

Leave a Reply