Modern C++ smart pointers and Tips to use

Smart pointer in Modern C++ are designed to manage dynamically allocated memory usage and deletion to ensure that the memory gets deleted automatically with triggers like scope closure. smart pointers were designed mainly to reduce the burden of manual memory deletion by checking the usage and scope closures. Before smart pointers came in to C++, it was developers responsibility to delete the memory by checking proper check points. This was very painful and causes a lot of memory leaks if developers miss to delete properly.

Smart pointers are just some classes wrappers on top of memory management logic. These classes provides pointer operators like -> (arrow operator) & (‘and’ reference operators), and * (asterisk operator) to assess the memory address and data.

So when were the smart pointers were introduced in C++ is a common question from C++ developers who were working with raw pointers for years. Smart pointers were introduced with C++ 11 standard of C++. Older version like C++ 98 does not have smart pointers concepts. The definitions and declarations of smart pointers are given in <memory> header of the Standard Library, so you must include this file in your code if you wish to use smart pointers.

How Many Types of Smart pointers are there in C++

First thing that comes to our mind is , how many types of smart pointers we have in C++. C++ have three types of smart pointers:

  1. std::unique_ptr
  2. std::shared_ptr
  3. std::weak_ptr

You can read more and see code examples about each of them by clicking on the above types.

Important tips to use smart pointers:

  1. Do NOT use or mix shared pointers and raw pointers together in same program or logic.
  2. Do NOT assign one raw pointer to multiple smart pointers.
  3. Do NOT get in to circular reference deadlock while using smart pointer.

Let us now see some code comparison of raw pointer usage and smart pointer advantages.

Comparison of a Raw Pointer vs Smart Pointer

Every developer wants to know , what is the difference between a pointer and a smart pointer. In this section we will try to explain this difference. In this code example, we are creating a class and then we are trying to use two functions. One function is using the traditional raw pointer to this class and second function will make use of the smart pointer.

#include <memory>
using namespace std;

class MemoryTest
{
    char TestData[36];
};

void RawMemoryPointer()
{
    MemoryTest* memTest = new MemoryTest("Raw Pointer Test");
	
    string TestName = memTest->TestData;
	
	memTest[0]->TestData; 	// This line will compile.
    memTest++;  			// This line will compile.
    memTest--;  			// This line will compile.
	
	if(memTest)
	{
		delete memTest;
		memTest = nullptr;
	}
}


void SmartMemoryPointer()
{
    unique_ptr<MemoryTest> memTest(new MemoryTest("Smart Pointer Test"));

    string TestName = memTest->TestData;
	
	memTest[0]->TestData; 	// This line will not compile.
    memTest++;  			// This line will not compile.
    memTest--;  			// This line will not compile.

} 

int main()
{
	RawMemoryPointer();
	SmartMemoryPointer();
	
	return 0;
}

How to use smart pointers in modern C++

Now we understood, What are smart pointers in C++ and how smart pointers are different from raw pointers. Next questions comes to our mind is, when should we use smart pointers in C++ and when we should use raw pointers. Let us understand a little about this. In the above two functions:

  1. First we are allocating memory to the pointer to the class that we are using. In each function we have used the raw pointer and smart pointer. First function is using the normal raw pointer memory allocation. In second function we are using the unique_ptr of smart pointer type.
  2. Then we are accessing the data in the variable of the class pointer. This you can see raw pointer function and smart pointer function code is same and using the -> arrow operator.
  3. Third line is the main difference in both the functions and both types of pointers. As you can see in raw pointer function we are deleting the memory by using the delete operator. If we don’t do that , it will cause a memory leak.
  4. On the other hand, in smart pointer function we are not deleting memory. Does this create a leak? The answer is no, because smart pointer will delete the memory by itself as soon as we come out of the function scope.