C++ 11 Smart pointers : shared_ptr tutorial and example

Smart Pointers are available from C++ 11.

Smart pointers are used to make sure that the object gets deleted if it is no longer used.

In this chapter we shall learn about “shared_ptr” concept.

We shall learn about below topics:

1. Introduction
2. How to create a shared_ptr object?
3. How to create an object using make_shared()?
4. Example 1
5. Example 2
6. Example 3
7. How to use make_shared()?

Introduction:

“shared_ptr” is one of the smart pointer class provided in C++ 11.

#include <memory> header to be included for smart pointers to work.

In is used to automatically delete the object created by “new” operator when it goes out of scope.

How to create a shared_ptr object?

std :: shared_ptr <int> p1 (new int());
std :: shared_ptr <myClass> p1 (new myClass());

To get the reference count, use “.use_count()”.

How to create an object using make_shared()?

std:: shared_ptr <myClass> p1 = std::make_shared<myClass>();

Points on shared_ptr:

1. “shared_ptr” are used when the object will be shred by multiple components.
2. “shared_ptr” has the ability to take the ownership of a pointer and share the ownership.
3. The object that a shared_ptr is pointed to is guarenteed to be deleted when all the shared_ptr pointing to is destroyed or reset.

Now let us understand “shared_ptr” with the help of numerous examples:

Example 1: Simple Example:

#include<iostream>
#include <memory> 
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

class MyClass
{
  
  public:
  MyClass()
  {
      cout<<"In constructor"<<endl;
  }
  ~MyClass()
  {
      cout<<"In destructor"<<endl;
  }
  
  
};


int main() 
{ 
    shared_ptr<MyClass> p (new MyClass);
    
    return 0; 
} 

output:

In constructor
In destructor

From the above example we can see that the object is created and then destroyed once it goes out of scope.

Example 2:

We created a function that will create an object and we shall call that function in main(). Let us see the O/P.

#include<iostream>
#include <memory> 
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

class MyClass
{
  
  public:
  MyClass()
  {
      cout<<"In constructor"<<endl;
  }
  ~MyClass()
  {
      cout<<"In destructor"<<endl;
  }
  
  
};

void fun()
{
    shared_ptr<MyClass> p (new MyClass);

}

int main() 
{ 
    cout<<"Entered main()"<<endl;
    fun();
    cout<<"Exited main()"<<endl;
    
    
    return 0; 
} 

Output:

Entered main()
In constructor
In destructor
Exited main()

Here we can see that, the program entered main(), created and deleted the object. Then exited the main fuction.

Example 3:

Let us modify the function in such that it will return the created object and check how the object lifetime behaves:

#include<iostream>
#include <memory> 
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

class MyClass
{
  
  public:
  MyClass()
  {
      cout<<"In constructor"<<endl;
  }
  ~MyClass()
  {
      cout<<"In destructor"<<endl;
  }
  
  
};

shared_ptr <MyClass> fun()
{
    shared_ptr<MyClass> p (new MyClass);
    return p;

}

int main() 
{ 
    cout<<"Entered main()"<<endl;
    shared_ptr<MyClass> p = fun();
    cout<<"Exited main()"<<endl;

    return 0; 
} 

Output:

Entered main()
In constructor
Exited main()
In destructor

Here the sequence has changed. We entered main and called the function fun. Now we return the object to main function. It means that the object ownership is changed from function “fun” to main function.

Hence the scope of the object is now the main function.

So when main() goes out of scope, the object destructor will be called.

Now how to get reference count?

You need to use “.use_count()” operator

Example:

#include<iostream>
#include <memory> 
//for more C++ tutorial visit www.ProDeveloperTutorial.com
using namespace std;

class MyClass
{
  
  public:
  MyClass()
  {
  }
  
  ~MyClass()
  {
  }
  
  
};

shared_ptr <MyClass> fun()
{
    shared_ptr<MyClass> p (new MyClass);
    return p;

}

int main() 
{ 
    shared_ptr<MyClass> p = fun();
    cout<<"Reference Count = "<< p.use_count()<<endl;
     
    shared_ptr<MyClass> p1 = p;
    cout<<"Reference Count = "<< p.use_count()<<endl;  
    
    return 0; 
} 

Output:

Reference Count = 1
Reference Count = 2

As you can see, the reference count gets increased. SO till the reference count is there, the object will not be deleted.

How to use make_shared()?

Instead of using new, you can use make_shared() to create a new object.

It is recommended to use “make_shared()” instead of new to create an object.

Now we shall modify the function “fun” as below:

shared_ptr <MyClass> fun()
{
    shared_ptr<MyClass> p = make_shared<MyClass>();
    return p;
}

All the O/P will remain same as we saw in previous example.

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *