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.
Table of Contents
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,
1 2 3 |
template< class T > unique_ptr<T> make_unique( std::size_t size ); |
1 2 3 |
template< class T, class... Args > unique_ptr<T> make_unique( Args&&... args ); |
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,
1 2 3 4 5 |
class myclass { }; |
We can create a unique pointer (a smart pointer) with this class as below,
1 2 3 |
std::unique_ptr< myclass > p = std::make_unique< myclass >(); |
Or it can be used with struct as below:
1 2 3 4 5 6 |
struct st_xy { int x,y; }; |
We can create a unique pointer as we illustrate in the example below.
1 2 3 |
std::unique_ptr< st_xy > xy = std::make_unique< st_xy >(); |
We can use the auto
keyword and we can define the maximum array elements like so:
1 2 3 |
auto ui = std::make_unique< int[] >(5); |
What are the advantages to use std::make_unique in C++?
The std::make_unique
function is useful when allocating smart pointer, because:
- We don’t need to think about
new
/delete
ornew[]
/delete[]
elisions. - When we use
new
we must mentionT
type twice, i.e.unique_ptr<T> up(new T(args))
, while we use make_unique with auto we don’t need to mention twice, i.eauto up = make_unique<T>(args)
mentions it once. - It prevents the unspecified-evaluation-order leak triggered by expressions like
myf( unique_ptr<X>(new X))
. - It is implemented for exception safety
- İt is recommended over directly calling
unique_ptr
constructors
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++:
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <iostream> #include <memory> struct st_xy { int x,y; }; class myclass { public: myclass() // Constructor { std::cout << "Class object is created." << std::endl; } ~myclass() // Destructor { std::cout << "Class object is destroyed." << std::endl; } }; void myf() { // Using make_unique with unique_ptr std::unique_ptr< myclass > o = std::make_unique< myclass >(); } int main() { std::unique_ptr< st_xy > xy = std::make_unique< st_xy >(); xy->x = 3; xy->y = 4; std::cout << "xy:" << xy->x << "," << xy->y << std::endl; auto ui = std::make_unique< int[] >(5); ui[0] = 01; ui[4] = 41; std::cout << "ui:" << ui[0] << "," << ui[4] <<std::endl; myf(); std::cout << "My function is done!" << std::endl; system("pause"); return 0; } |
The output from the above example code will be as show below.
1 2 3 4 5 6 7 8 |
xy:3,4 ui:1,41 Class object is created. Class object is destroyed. My function is done! Press any key to continue . . . |
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
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition