In this article, We will learn what are shared pointers and how it is used, how we create shared pointers and where we should use them, where we should not use them. What are the advantages of using shared pointers.
so let us begin with what are shared pointers.
What is Shared_ptr in C++ and its advantages
std::shared_ptr is a smart pointer that holds shared ownership of an object using a pointer. At a time many shared_ptr objects may point to the same object. The shared_ptr allows you to make a copy of the pointer. shared_ptr does this by maintaining a reference count every time a copy is created.
Do we need to delete the shared pointers? The answer is No. shared_ptr is destroyed automatically and its memory deallocated when either of the following conditions met:
- The reference count of the shared_ptr goes to 0, means the all shared_ptr references are destroyed.
- All shared_ptr references or the last reference of shared_ptr is moved to another pointer by using == operator or reset function of shared pointer class.
- The shared pointer is deleted by a custom deleter or delete expression associated with that pointer.
shared_ptr can be created as empty and in this case it will not point to any address. shared_ptr needs to be used carefully when it comes to usage by multiple processes or threads. We will cover the race conditions with shared_ptr in separate article.
Syntax : How to create and use shared_ptr in C++
The syntax to create a unique pointer is:
shared_ptr NameofPointer(new Data Type);
or
shared_ptr NameofPointer
Where:
shared_ptr : This is the syntax to specify that we are creating a shared_ptr of a specied Data Type. We are passing the Data
Types in the <> brackets.It can be a standard data type like int, float or a user defined data type like a class, structure etc.
NameofPointer : This is the Name of the pointer variable that you want to give it to. (new Data Type) : This is the memory allocation done by using the new operator and the data type so the proper size can be allocated on heap same as the size of data type.
Now let us see this shared_ptr in some code examples. The first thing to keep in mind is we must include the or header file to get the shared_ptr declaration in our program.
shared_ptr C++ Code example
//Code Example using shared_ptr
#include <iostream>
#include <memory>
using namespace std;
void Pointer_function()
{
// Declaration of a shared_ptr of type int with a int raw pointer.
shared_ptr<int> ptr(new int(63));
if(ptr)
{
cout<< " This is a shared_ptr pointer of type int";
}
// Declaration of a shared_ptr which is empty.
shared_ptr<int> second_ptr;
if(second_ptr == nullptr)
{
cout<< " This is a null or empty pointer of type int";
}
second_ptr = ptr;
}
int main()
{
Pointer_function();
return 0;
}
How smart pointer shared_ptr is used
In above example code, we are first declaraing a shared_ptr of type int and we are passing a raw pointer of type int as argument. This is make the reference count to 1. second declaration that we are doing is also a shared_ptr, but this time we didn’t not pass any argument so it will create an empty shared_ptr which is not pointing to any memory.
At the end we are assigning pointer 1 to pointer 2 , which is allowed in case of shared_ptr. Now they both point to same memory location and share the data. This way we now have 2 pointers and the reference count also increment to 2. As soon as the Pointer_function() ends(both smooth exit or exception exit), both the pointers will go out of scope and any memory associated with them will be deleted and freed. This is done by the destructor of the shared_ptr which does this clean up for us. So we don’t need to do explicit memory clean up when we use the smart pointers.