Smart Pointers implementation in C++ with examples

A smart pointer is a pointer that is used to handle the problems that are caused by using the normal pointers

The output of the above program is

Here we need to delete the object that is used dynamic memory using delete. If we forgot to delete the object, we will get a memory leak. The main problem that comes with the pointers is memory management.
To overcome these types of problems that are caused by pointers, smart pointers are used.
Bu using smart pointers, we can make pointers work in a way that we don’t need to explicitly call delete.
A smart pointer is a wrapper class above a pointer with operators like * and -> overloaded. The objects of the smart pointer class look like pointer but can do many things that a normal pointer can’t like automatic destruction, reference counting, and more. Here we need to create the pointer object of the class because the destructor and the overloaded operators

are called automatically when the object goes out of scope, the memory allocated for that object is automatically deleted.
Let us look into the small example to understand more about the smart pointers

The output of the above program is:

In the above program We don’t need to call delete pcPtr: when the object pcPtr goes out of scope, destructor for it is automatically called and destructor does delete pcPtr.

To delete any type of the object we need to create a template class for the smart pointers.
This template class accepts any type of object as an argument. And deletes the dynamic memory automatically.

Example for the interface class:

See the below sample class that contains the overloaded operator functions

Example for the smart pointer interface:

Calling the smart pointer in the main

For the above example, we can only delete only one class object.
If we can write the smart pointer class more generically, it will be more use for us to handle many classes. Therefore, to overcome the burden generic smart pointers are possible.

Below is the generic smart pointer with the example code:

Now we can use our smart pointer class for any type of pointer. Fine till now

But, see the below case, which is very dangerous

See here ocStudent and ocStudent2 are referring to the same MyStudentCLs class pointer.
Now when ocStudent2 goes out of scope, the destructor of ocStudent2 will be called which deletes the MyStudentCLs class pointer. Now we cannot call

since ocStudent will be left with a dangling pointer and this call will fail. (Note that this problem would have existed even if we were using normal pointers instead of smart pointers.) We should not delete the MyStudentCLs class pointer unless no body is using it. How do we do that? Implementing a reference counting mechanism in our smart pointer class will solve this problem.

Example program that implement the Reference counting using the smart pointers:

Reference counting calss:

Mystudent class:

In Our We need to create the a pointer to class MyocRefCountingCls in our MySmartPointerCls class and this pointer will be shared for all instances of the smart pointer which refers to the same pointer. For this to happen, we need to have an assignment operator and copy constructor in our SP class.

The output of the above program is:

Using the smart pointers, we can reduce the risk of managing the pointers, this leads to our code without memory leaks.