Site icon Learn C++

What Is make_unique And How To Use With unique_ptr In C++

What Is make unique And How To Use With unique ptr In C++

Allocating memory and freeing it safely is hard if you are programming a very big application. In Modern C++ there are smart pointers that help avoid the mistake of incorrectly freeing the memory addressed by pointers. Smart pointers make it easier to define and manage pointers, they came with C++11. The most used types of C++ smart pointer are unique_ptr, auto_ptr, shared_ptr, and weak_ptr. In C++14, there is make_unique (std::make_unique) that can be used to allocate memory for the unique_ptr smart pointers.

What is a smart pointer and what is unique_ptr in C++?

In computer science, all data and operations during runtime are stored in the memory of our computation machines (computers, IoTs, or other microdevices). This memory is generally RAM (Random Access Memory) that allows data items to be read or written in almost the same amount of time, irrespective of the physical location of data inside the memory.

Allocating memory and freeing it safely is really hard if you are programming a very big application. C and C++ were notorious for making this problem even more difficult since earlier coding styles and C++ standards had fewer ways of helping developers deal with pointers and it was fairly easy to make a mistake of manipulating a pointer which had become invalid. In Modern C++, there are smart pointers that help avoid the mistake of incorrectly freeing the memory addressed by pointers which was often the source of bugs in code which could often be difficult to track down. Smart pointers make it easier to manage pointers, they came with the release of the C++11 standard. The most used types of C++ smart pointer are unique_ptr, auto_ptr, shared_ptr, and weak_ptr.

std::unique_ptr is a smart pointer that manages and owns another object with a pointer, when the unique_ptr goes out of scope C++ disposes of that object automatically. If you want to do some magic in memory operations use std::unique_ptr. Here are more details on how you can use unique_ptr in C++,

What is std::make_unique in C++?

The std::make_unique is a new feature that comes with C++14. It is used to allocate memory for the unique_ptr smart pointers, thus managing the lifetime of dynamically allocated objects. std::make_unique template constructs an array of the given dynamic size in memory, and these array elements are value-initialized.

Since C++14 to C++20, the std::make_unique function is declared as a template as below,

[crayon-663cf95208664284609957/]
[crayon-663cf9520866b395093076/]

The std::make_unique function does not have an allocator-aware counterpart; it returns unique_ptr which would contain an allocator object and invoke both destroy and deallocate in its operator().

How to use std::make_unique with std::unique_ptr in C++?

Let’s assume you have a simple class named myclass,

[crayon-663cf95208672380657095/]

We can create a unique pointer (a smart pointer) with this class as below,

[crayon-663cf95208674319810735/]

Or it can be used with struct as below:

[crayon-663cf95208676886031761/]

We can create a unique pointer as we illustrate in the example below.

[crayon-663cf95208678548940102/]

We can use the auto keyword and we can define the maximum array elements like so:

[crayon-663cf95208681652714437/]

What are the advantages to use std::make_unique in C++?

The std::make_unique function is useful when allocating smart pointer, because:

In addition, if you need a custom delete operations for your type object or if you are adopting a raw pointer from elsewhere, do not use make_unique.

Is there a full example of how to use std::make_unique with std::unique_ptr in C++?

Here is a full example of how to use std::make_unique with std::unique_ptr in modern C++:

[crayon-663cf95208683324174871/]

The output from the above example code will be as show below.

[crayon-663cf95208685338895379/]

For more information about this feature, please see https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3656.htm

and the Proposal of std::make_unique, https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3588.txt

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here

Exit mobile version