C++C++11C++14C++17C++20Learn C++Syntax

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,

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,

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

Or it can be used with struct as below:

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

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

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 or new[]/delete[] elisions.
  • When we use new we must mention T 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.e auto 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++:

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

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

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

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++

Artificial Intelligence TechC++C++11C++14C++17Learn C++

How To Develop Special AI Activation Functions In C++?

Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?