C++

C++ std::make_unique

In this article, we will be discussing a function “make_unique” that is provided by the memory library. A memory library is used to define utilities for managing the memory allocation and various tasks in the memory heap. Before moving to our topic, we should know about what unique pointers are because the use of the make_unique function also includes the unique pointers too. A unique pointer is a smart pointer which has ownership of any pointer but does not share it with any other pointer. The make_unique function is the more efficient way of initializing the unique_pointer and it returns a unique pointer to any object that has a specified type. It was introduced in C++ 14. When using the make_unique function to create a unique_pointer of array type, then we have to declare the elements of the array separately.

Syntax:
The following is the syntax of the make_unique pointer:

template<class t> unique_ptr<t> make_unique<t>(args);

In the syntax above “class t” is the type to which the unique pointer will be pointing and “args” denotes the argument that is to be passed to the constructor of any object. Another parameter that is sometimes passed to this function is “size” which is used to allocate the memory heap to the number of elements that are being passed to it. The type of the object for the make_unique function is passed in angle brackets where the values to which the object pointer points are passed in parenthesis.

Example # 01:

Let us perform an example in which we will create an object which will print the message for the user in the confirmation of whether the make_unique is successfully created a unqiue_ptr or not. Now, including the header files, the first one is the memory and the second one is the isotream. Memory is used for providing the utilities for managing dynamic memory allocation. The iostream is used to perform various input-output operations. After including our header files, we will create a class named the “mkunique”. Inside this class, we create a function named “show()”. For handling the message display on the object, call using the make_unique pointer.

The show() method will be called using the unique pointer that we have declared inside our main function. Now, proceeding to the main function where we initialized a unique pointer “p” of type and “mkunique” class to which we assigned a make_unique member function of type “mkunique”. After that using that pointer “p”, we will call the show() method using the “->” operator which is used to access the function of the object class using pointers.

#include <iostream>
#include <memory>
class mkunique
{
public:
    void show()
    {
        std::cout <show();
}

Let us check out our output in which the message is successfully displayed. This means we can create a unique pointer using the make_unique member function.

Example # 02:

Now, we will be performing another example in which we will create an array using a smart pointer and will get the values that are passed in it. After including our header files, as were in the above example, the iostream and the memory header file we will dive into our main function. We have declared an auto pointer “mk_unique”, an auto is a data type that is used to declare a pointer type variable. To the mk_unique, we assigned the “make_unique” member function of the type array, where “8” is the size of the array. It means the mk_unique variable is now a unique pointer that will be handling the array of size less than “8”.

Next, using the for loop, we are going to store the values in our array using the variable “k”. The for loop starts from “0” and ends at the value “7”. It will start from 0 to less than 8 until the last index is met. It will execute the inner code with the increment of 1. Inside the for loop, we assigned “k” to the pointer which will store the values in the memory heap. And then using the “cout” statement, we displayed these values using the make_unique pointer “mk_unique”.

#include <iostream>
#include <memory>

int main()
{
    auto mk_unique = std::make_unique<int[]>(8);
for (int k = 0; k < 8; ++k)
{
    mk_unique[k] = k;
    std::cout << mk_unique[k]  << std::endl;
}
}

As shown in the snippet below, the values that we passed to the pointer array printed the values from 1 to 7 because the array index starts from 0 so from 0 to 7 stores the values that we passed using the for loop are from 1 to 7. When the loop starts for the first time, it will check the condition whether k is less than “8” or not. Then, it executes the value that is stored in the index 0 which is 1. After that, it will increment it by 1 and so on until the condition is false.

Example # 03:

Now, we will try a simple way to create a unique pointer using the make_unique member function. Then, we will print the value using the pointer that we will be creating. Let us first include header files then head toward our main function in which we have declared a unique pointer named “p” of type integer. This means it will be holding the integer type value. To that pointer, we assigned a make_unique pointer of integer type to which we passed the value “123” as an argument which will be stored in the memory heap. In the end, we have displayed the value that is stored in the memory using “*p” which means it will be displaying the value of the unique pointer “p”, asterisk “*” is used to access the pointer.

#include <iostream>
#include <memory>
{
    std::unique_ptr<int> p = std::make_unique<int>(123);
    std::cout << *p;
}

As shown in the figure below the output of the code performed above is displayed. We displayed the value of the unique pointer “p” that was “123” without having any error.

Conclusion

In this guide, we have explained the methods of how the make_unique member function is used to create a unique_pointer. Make_unique is the more effective way to declare a pointer of a unique type, these pointers are destroyed whenever it moves out of the scope. Make_unique is the safest way to create a temporary memory location in the memory heap.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.