What is std::unique_ptr? How to Use std::unique_ptr?

std::unique_ptr is a smart pointer that owns and manages another object through pointer. std::unique_ptr has unique ownership of the object. That means only one std::unique_ptr can have the ownership of the actual object. We can not assign a std::unique_ptr object to another smart pointer.

std::unique_ptr disposes the managed object when the unique_ptr goes out of context or is assigned with nullptr. It takes care of the common mistake that programmers make of forgetting to delete dynamically allocated objects. Sometimes deleting object is also cumbersome when there are multiple return paths in a function. You need to delete the object from multiple locations. Forgetting to delete the allocated object leads to memory leak.

If you use std::unique_ptr, you don’t need to worry about freeing the object.

Example of Simple Usage of std::unique_ptr

#include <iostream>
#include <memory>

class C {
public:
  C() {
    std::cout << "Constructing the class..." << std::endl;
  }

  ~C() {
    std::cout << "Destructing the class..." << std::endl;
  }

  void f() {
    std::cout << "Printing from function f()..." << std::endl;
  }
};

int main() {

  std::unique_ptr<C> pc = std::unique_ptr<C>(new C());
  pc1->f();
  return 0;
}

We created an unique_ptr of type C by “std::unique_ptr pc = std::unique_ptr(new C());“. As std::unique_ptr is a template function, we have to specify the type C. We passed a pointer of an object of class C. Once the smart pointer (pc) is created, we can use that just like a pointer of class C. Like we called the function f().

Here we don’t need to free the actual object. It will get freed when the smart pointer pc will go out of context.

To compile the program, run this command.

g++ test.cpp -std=c++11

Here is the output the program.

Constructing the class…
Printing from function f()…
Destructing the class…

From the output, we can see that the destructor gets called even though we did not delete the object explicitly.

Even though we can use the unique_ptr, pc, as normal pointer, it is actually an object of type std::unique_ptr<C>. When this object goes out of context, the destructor of pc gets called. From there the actual pointer of C deleted. By default, the C++ delete operator gets called. But you can use you own custom deleter also.

Also this smart pointer, pc, owns the C type object uniquely. That means that we can not assign this pointer to another smart pointer of the same type.

std::unique_ptr pc = std::unique_ptr(new C());
std::unique_ptr pc1 = pc;

The above code will generate compilation errors. But you’ll be able to move the actual pointer to another std::unique_ptr.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

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

1
0
0
0
1
0