Memory problems is one of the biggest challenges facing by many developers in using of C++ pointer. Common mistakes like,

1. Forgetting to call delete or delete[] after memory were allocated.
2. Deleting of unreference pointer, etc.

will cause memory leakage problems in your C++ application. Even worst, it may crash your application.

To avoid common memory problems, you should use smart pointers instead of normal pointers. You don’t have to call delete or delete[] after memory were allocated. Smart pointers will automatically de-allocate memory when the smart pointer object goes out of scope. Smart pointer is now avaiable with the latest ISO C++ standard.

There are 2 types of smart pointer template, shared_ptr and unique_ptr. You use share_ptr if you would like to make it able to reference by other smart pointer. If you don’t plan to make it able to reference by other, then you need to declase it as unique_ptr. The compiler will complain if you try to assign a unique_ptr to other smart pointer.

Using of smart pointer is simple. The following demonstrate a simple C++ program on how to use the smart pointer.

You will need to include C++ memory header file in order to utilize smart pointer.

#include <memory>

Let’s create a Sample class that take only 1 integer value in its constructor. Also, create a get_num function to return the integer value.

1
2
3
4
5
6
7
8
class Sample
{
public:
  Sample(int value){ number = value; }
  int get_num(){ return number; }
protected:
  int number;
};

Type the following in the main function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
  std::shared_ptr<Sample> p1 = std::make_shared<Sample>(3);
  std::unique_ptr<Sample> p2(new Sample(4));
  auto p3 = p1;

  // the compiler will complain if you try to assign p4 to a unique_ptr
  //auto p4 = p2;


  std::cout << "p1->get_num(): " << p1->get_num() << std::endl;
  std::cout << "p2->get_num(): " << p2->get_num() << std::endl;
  std::cout << "p3->get_num(): " << p3->get_num() << std::endl;

  return 0;
}

Notice that we don’t call delete to deallocate the memory being reserved by smart pointer. Compile and run the program now and you will observe the following output:

p1->get_num(): 3
p2->get_num(): 4
p3->get_num(): 3

p1 is a share pointer where it allow p3 to reference to it. In this case, the output od p1 & p3 is identical. p2 is a unique pointer which doesn’t allow to reference by other. The compiler will complain if you try to enable the following line of code in the main function.

//auto p4 = p2;

Sample C++11 Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <memory>

class Sample
{
public:
  Sample(int value){ number = value; }
  int get_num(){ return number; }
protected:
  int number;
};

int main()
{
  std::shared_ptr<Sample> p1 = std::make_shared<Sample>(3);
  std::unique_ptr<Sample> p2(new Sample(4));
  auto p3 = p1;

  // the compiler will complain if you try to assign p4 to a unique_ptr
  //auto p4 = p2;


  std::cout << "p1->get_num(): " << p1->get_num() << std::endl;
  std::cout << "p2->get_num(): " << p2->get_num() << std::endl;
  std::cout << "p3->get_num(): " << p3->get_num() << std::endl;

  return 0;
}