Smart Pointers in C++ With Examples

In this article, we’ll take a look at how we can use Smart Pointers in C++.

Smart Pointers are an abstract interface to actual pointers (also called raw pointers), but with the additional benefit of automatically managing resources and freeing memory.


Smart Ptr Example

The design logic for a smart pointer is to implement it as a class.

This is because a class has a destructor, which will trigger automatically when an object is at the end of its scope. Smart Pointers use the destructors to free up resources automatically.

You don’t need to free smart pointers explicitly, and hence they are “smart” pointers since they know when they need to free resources.

Let’s understand more about these types of smart pointers in C++, using examples.

Using Smart Pointers in C++

These pointers are defined in the standard C++ library, in the std namespace. To include some of these pointers, we also need the <memory> header file.

There are three commonly used smart pointers, called:

  • unique_ptr
  • shared_ptr
  • weak_ptr

Let’s go through them one by one.

unique_ptr – Smart pointer in C++

This is a smart pointer that will only permit one owner of the pointer. That is, unique_ptr can contain, at maximum, only a single raw pointer (“unique” pointer) that points to a single memory location.

Keep this as your default choice, unless you’re working with multiple threads, or whenever you need multiple owners to the same raw pointer.

To create a smart pointer using unique_ptr, there are two ways of achieving this:

Case 1: Creating a Unique Smart Pointer from an object

If you have an object and want to create a smart pointer in C++ which points to this object, use the below syntax:

The above line creates a new raw pointer of type MyTyp. We pass this to create our smart pointer using std::unique_ptr<myType>.

You can now call methods on the object using the smart pointer handle, using the -> operator.

You can also free the memory that my_ptr owns, using:

We use the . operator, since we apply this to the smart pointer directly.

Case 2: Construct a new unique pointer to a memory location

If you want to construct a new unique pointer, simply use the make_unique() function.

This creates a unique pointer to the string “Hello from JournalDev”.

Other operations remain the same.

Move a unique pointer

Since a unique_ptr is unique for any memory location, we cannot have a unique pointer to multiple objects. So we can only move a unique_ptr.

Let’s now look at an example to see how the whole thing works:



The shared_ptr is another choice for a smart pointer in C++, if you want to deal with multiple owners. This also maintains a reference count of all the pointers which point to the object.

Similar to unique_ptr, the syntax is almost the same, except that you now return a shared pointer instead. You are also now allowed to pass multiple objects to the invocation, so that the shared_ptr points to all of them.

To construct a new shared pointer, use the make_shared() function.

There is no requirement of using std::move, since a shared pointer can point to multiple locations.

To get the current reference count for any/all of the shared pointers, use my_shared_ptr.use_count(). This will automatically update when we create or free a shared pointer to the common object.

We’ll look at an example regarding shared_ptr.


As you can observe, both pointers point to the same object. After any one of them is freed, again, the reference counts are updated to reflect the changes.


The weak_ptr is a smart pointer in C++ that is similar to shared_ptr, but it does not maintain a reference count. This is useful in cases where objects of one class can point to another, and vice-versa.

Consider the following scenario, where you have two classes A and B, having a shared_ptr member to the other class (i.e an object of A can point to an object of B, and vice-versa). Now, when you create two objects of A and B, make them point to each other, using the shared_ptr member, what happens now?


Yes, we don’t get any output, even though we expect our destructor to clean-up for us! What is happening?!!

This is an example of a Circular Reference, where pointers point to each other. Since these pointers are shared_ptrs, they also have their reference counts as 2.


Circular Reference Shared Ptrs

When the destructor of the ptr_a‘s object tries to clean-up, it finds out that ptr_b is pointing to ptr_a, so it cannot simply remove it. Similarly, the destructor for ptr_bs object also cannot clean-up, resulting in no output!

To eliminate this problem, we can use the weak_ptr smart pointer inside our class. Since the weak pointer does not contribute to the reference count, we can make one of the two pointers as a weak pointer, for example, ptr_b to ptr_a.

Since a weak pointer does not own the data, we can now clean up the shared pointer a, after which we can clear b also!

So, we can make the ptr member of class B as a weak_ptr. (You could also do it for A, but we’ll do it for B)


Resolve Dependency

Let’s now make the changes to our code snippet to make this work.


Now, we’ve changed the type of the smart pointer of class B to weak_ptr. So now, there is no more circular dependency, and we’ve finally solved this problem!

General Programming Guidelines

To summarize this article, I’ll provide you with a small list of guidelines to follow when working with SMART pointers in C++.

  • Always try to use smart pointers whenever you can. In most cases, use unique_ptr, if you’re not dealing with sharing a memory location with multiple pointers/threads.
  • Otherwise, use the reference-counted shared_ptr, when dealing with multiple owners.
  • If you want to examine an object, but not require that the object itself exists, use a weak_ptr. This pointer does not contribute to the reference count and is suitable for such tasks.
  • Whenever you directly use raw pointers, try to make sure it is contained only in a very small amount of code, or where you must absolutely use it, to reduce the overhead of using smart pointers.


In this article, we learned how we could use smart pointers in C++, along with some examples for each type.

For more articles, go through our C++ tutorials section! Do leave any feedback in the comment section below!


  • Microsoft Documentation on Smart Pointers (Is a very good resource, even if you’re working on Linux. Highly recommended read)

By admin

Leave a Reply