What is a smart pointer and when should I use one in C++

Hey learners, In this tutorial we will learn everything about smart pointers in C++ so stay tuned.

Do we all know what is a pointer?? Pointers are just like variables that are used to store addresses. We can dynamically create a pointer using a new operator and can also deallocate it using delete. But what if you forgot to deallocate it, it can cause a memory leak.

These allocation and deallocation processes are a huge overhead for beginners because you have to do it manually.

Is there any smarter way to do that?

Yes, here the concept of a smart pointer comes into the picture. Let’s understand it in detail.

Smart pointer in C++

Smart pointers are pointers for which we don’t need to deallocate memory using delete, it can be done automatically. If we consider other languages like java etc…it has a garbage collector which can deallocate the unused memory. Similarly, in C++ we can achieve this using smart pointers.

Why use a smart pointer??  if you want to make efficient use of memory smart pointer will be a great choice for you.

Code

#include <iostream>
using namespace std;

class smartpointer {
  float* ptr1;
public:

    smartpointer(float* p = NULL)
  {
      ptr1 = p;
    }


  ~smartpointer()
  {
      delete (ptr1);
    }


  float& operator*()
  {
         return *ptr1;
    }
};

int main()
{
  smartpointer ptr1(new float());
  *ptr1 = 80.44;
  cout << *ptr1;


  return 0;
}

Explanation

  • Here we are creating a class named smartpointer. After that, we will create a float pointer named ptr1.
  • Inside our class we will create a constructor, destructor and we will overload dereferencing operator.
  • here we don’t need to deallocate memory using delete this can be automatically done by the destructor. At last, it will print the value.

      Types of smart pointers

  • Unique_ptr
  • Shared_ptr
  • Weak_ptr

Let’s understand each one of them.


 1) Unique_ptr:- Unique pointers are those in which only one pointer can point an object at a time.

  Example

  • Firstly we need to include header file memory.
  • we will create a class square inside that we will make a constructor and a function to calculate the area.
  • Inside our main function we will create a unique_pointer and call the area function for that which will calculate the area and print it
  • Remember it’s a unique pointer which means only one pointer can be used at a time so if want to make another pointer we will first remove pointer1 this can be done using the move function.

        Code

#include <iostream>
using namespace std;
#include <memory>

class Square {
  int side;


public:
  Square(int s){
    side=s;
  }

  int area(){
    return side*side;
  }
};

int main(){

  unique_ptr<Square> Pointer1(new Square(5));
  cout << Pointer1->area() << endl;

  unique_ptr<Square> Pointer2;
  Pointer2 = move(Pointer1);


  cout << Pointer2->area() << endl;


  return 0;
}

2) Shared_ptr:- Shared pointers are those in which more than one pointer can point an object at the same time. Additionally, it will maintain a  reference counter using the use_count method.

Example

  • Firstly we need to include header file memory.
  • we will create a class square inside that we will make a constructor and a function to calculate the area.
  • Inside our main function we will create a shared_pointer and call the area function for that which will calculate the area and print it.
  • Remember it’s a shared pointer which means more than one pointer can point an object at the same time.
  • lastly we will use a method named use_count.

  Code

#include <iostream>
using namespace std;
#include <memory>

class Square {
  int side;


public:
  Square(int s)
  {
    side=s;
  }

  int area()
  {
    return side*side;
  }
};

int main()
{

  shared_ptr<Square> Pointer1(new Square(5));

  cout << Pointer1->area() << endl;

  shared_ptr<Square> Pointer2;
  Pointer2 = Pointer1;

  cout << Pointer2->area() << endl;


  cout << Pointer1.use_count() << endl;
  return 0;
}


3)Weak_ptr:- Weak_pointer are same as shared pointer the only difference is that it will not maintain reference counter.

Example

  • Firstly we need to include header file memory.
  • we will create a class square inside that we will make a constructor and a function to calculate the area.
  • Inside our main function we will create a shared_pointer1 and a weak_ptr.
  • lastly we will use a method named use_count but it will not maintain reference count as it is a weak_ptr.

    Code

 

#include <iostream>
using namespace std;
#include <memory>

class Square {
  int side;


public:
  Square(int s)
  {
    side=s;
  }

  int area()
  {
    return side*side;
  }
};

int main()
{

  shared_ptr<Square> Pointer1(new Square(5));

  cout << Pointer1->area() << endl;


    weak_ptr<Square>  Pointer2 = Pointer1;
    cout<< Pointer1.use_count()<<endl;



  return 0;
}



That’ll about Smart pointers To conclude I would say Smart pointers are a great choice if you want to make efficient use of available memory.

All the 3 types of smart pointers have their importance it depends on the user’s need where he/she wants to use it.

Leave a Reply

Your email address will not be published.