C++

Smart Pointer in C++

Smart pointer is a very important concept introduced in C++. Smart pointer is used in C++ to resolve the problem of using normal pointer. To remove the drawbacks of the normal pointer, smart pointer exists in the C++.

Smart Pointer vs Regular Pointer

The two major problem of using regular pointers are:

a. Regular pointer cannot manage memory utilization efficiently.

b. It cannot release the memory object when they are not used in the program.

c. As the normal pointer cannot de allocate the memory of the object, if the pointer is removed from the program, the address of the memory object which is point out by the pointer is not found. As a result, memory leak happens.

For this, we use the smart pointer over the regular pointer. The advantages of smart pointers over regular pointers are:

a. It manages memory automatically.

b. It released the memory of the object when they are not used in the program.

c. It de allocates the memory of the object when the pointer goes out of the scope in the program.

d. Smart pointer is used in C++ for allocating the objects, traversing in the different types of data structure and manages different types of lambda expression for passing inside the function.

e. It makes our program very secure and safe. As a result, the program turns to be very simple to understand and become easier to debug.

Different Types of Smart Pointers

Normally, there are three types of smart pointers available in C++. They are:

a. Unique

b. Shared

c. Weak.

We will discuss each of them below.

a. Unique Pointer

a. Unique pointer holds a pointer to an object. It releases the memory of the object when it goes out of the scope.

b. One of the unique features of the unique pointer is that there is only one copy of an object present in the memory. No other resources can point to that particular object.

c. If many resources are available for one object in the code, it occurs a compile time error.

Programming Example 1:

#include <iostream>

#include <memory>

using namespace std;
class Square {
int side;
public :
Square (int s)
{
side = s;
}
int area ()
{
    return (side*side);
}
};
int main()
{
unique_ptr P1(new Square(2));
cout< area () <<endl;    // // introducing unique pointer;

return 0;
}

Output:

Explanation:

Here, we created a class named Square. Inside the class, a variable side is declared and calls the constructor to initialize the value of the side variable. Now, we defined a function named area which returns its area value.

Inside the main () function, we declared a unique pointer named unique_ptr. Now, we created a pointer P1 which points the object of the class Square and inside its parenthesis, we pass a value 2.

Now if we print the area through the pointer P1 like P1->area(), it shows the area of the square is 4.

b. Shared Pointer

a. Shared pointer can be applied in the program when we want to assign one pointer to multiple object resources.

b. Shared pointer is an address generated counting smart pointer, which can be used to store and pass a reference beyond the scope of a function.

c. It is very useful in OOP (Object Oriented Program). To store a pointer as a member variable, shared pointer is used.

d. Shared pointer will not be deleted until all the resources have completed their task.

Programming Example 2:

#include <iostream>

#include <memory>

using namespace std;
class Square {
int side;
public :
Square(int s)
{
side = s;
}
int area ()
{
    return (side*side);
}
};
int main()
{
shared_ptrP1(new Square(2));
// introducing shared pointer;
shared_ptrP2;
P2 = P1;
cout<area()<<endl;
cout<area()<<endl; // both object shows same result.
return 0;
}

Output:

Explanation:

This programming example 2 is the continuation of the programming example 1. Inside the main() function, we introduced the shared pointer. Using the pointer P1, we created the object of the Square class. Same object is pointed by the value P2->area() and P1->area(). Both shows the area of the square is 4.

c. Weak Pointer

a. Weak pointer is a special case of pointer to be used with the shared pointers.

b. Weak pointer has facility to access an object that is owned by one or more shared pointer instance.

c. It is not a part of reference counting.

d. We use the weak pointer in the program when we want to observe an object, but don’t require it to remain alive.

Programming Example 3:

#include <iostream>

#include <memory>

using namespace std;
class Square {
int side;
public :
Square(int s)
{
side = s;
}
int area ()
{
    return (side*side);
}
};
int main()
{
shared_ptrP1(new Square (2));
weak_ptrw1;
weak_ptr w2(w1);
weak_ptr w3(P1);
cout<< "w1:" << w1.use_count()<<endl;
cout<< "w2:" << w2.use_count()<<endl;
cout<< "w3:" << w3.use_count()<<endl;
return 0;
}

Output:

Explanation:

This programming example 3 is the continuation of the programming example 2. Here, we introduced a shared pointer named shared_ptr and created a pointer P1 to point the object of the Square class. Now we used the weak pointer, weak_ptr which points w1 and w2. Inside the w2, we pass w1. We created another weak pointer w3 where we pass the pointer P1.

Now if we print all the w1.use_count() and w2.use_count(), the result will display.

Conclusion

In discussing about the concept and uses of smart pointer in detail, we have come to this conclusion that smart pointer is introduced in C++ to remove the drawbacks of the normal pointer. Through the smart pointer, we can manage different types of owners and resources of the object very efficiently. We hope this article is helpful. Check out other Linux Hint articles for more tips and tutorials.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com