Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating Unique Pointers | Unique Pointers
course content

Course Content

C++ Smart Pointers

Creating Unique PointersCreating Unique Pointers

Technically, there are two ways to create a unique pointer. However, one of those ways is widely discouraged by C++ experts. For the sake of learning, we will explore both ways.

The recommended way – using std::make_unique

The std::make_unique, introduced in C++14, is a function that provides a safe and efficient way to create unique pointers. It's a great alternative to using the new/delete and new[]/delete[] operators, which can make C++ code more susceptible to memory-related issues.

cpp

makeunique.cpp

In the above code, std::make_unqiue allocates the dynamic resource and returns a unique pointer that owns it. When the unique pointer goes out of scope, the resource is automatically deallocated. Moreover, it’s designed to be exception-safe, which eradicates the chances of resource leaks due to exceptions.

Note

Don’t use std::make_unique when you are specifying a custom deleter for your unique pointer. Custom deleters are an advanced concept that we will cover later in this course.*

The frowned-upon way – Direct initialization

You can also create a unique pointer directly by initializing it with the result of the new operator. However, this approach is discouraged, due to potential resource leaks in case an exception is thrown.

cpp

newUniquePtr.cpp

The vector in the above code will still be automatically destroyed when the unique pointer goes out of scope. However, for maximum exception safety and better code practices, always prefer using std::make_unique.

Managing custom objects with unique pointers

Unique pointers are not limited to primitive data types (like integers) or standard containers (like vectors). They can also be used to manage dynamically allocated custom objects.

cpp

customObjUniquePtr.cpp

In the above code, we are managing a custom object using a unique pointer. As soon as the object goes out of scope, the unique pointer calls its destructor to deallocate it. Read the code comments to know exactly what is happening!

Does this code follow best practices that we have discussed?

Select the correct answer

Everything was clear?

Section 2. Chapter 2
course content

Course Content

C++ Smart Pointers

Creating Unique PointersCreating Unique Pointers

Technically, there are two ways to create a unique pointer. However, one of those ways is widely discouraged by C++ experts. For the sake of learning, we will explore both ways.

The recommended way – using std::make_unique

The std::make_unique, introduced in C++14, is a function that provides a safe and efficient way to create unique pointers. It's a great alternative to using the new/delete and new[]/delete[] operators, which can make C++ code more susceptible to memory-related issues.

cpp

makeunique.cpp

In the above code, std::make_unqiue allocates the dynamic resource and returns a unique pointer that owns it. When the unique pointer goes out of scope, the resource is automatically deallocated. Moreover, it’s designed to be exception-safe, which eradicates the chances of resource leaks due to exceptions.

Note

Don’t use std::make_unique when you are specifying a custom deleter for your unique pointer. Custom deleters are an advanced concept that we will cover later in this course.*

The frowned-upon way – Direct initialization

You can also create a unique pointer directly by initializing it with the result of the new operator. However, this approach is discouraged, due to potential resource leaks in case an exception is thrown.

cpp

newUniquePtr.cpp

The vector in the above code will still be automatically destroyed when the unique pointer goes out of scope. However, for maximum exception safety and better code practices, always prefer using std::make_unique.

Managing custom objects with unique pointers

Unique pointers are not limited to primitive data types (like integers) or standard containers (like vectors). They can also be used to manage dynamically allocated custom objects.

cpp

customObjUniquePtr.cpp

In the above code, we are managing a custom object using a unique pointer. As soon as the object goes out of scope, the unique pointer calls its destructor to deallocate it. Read the code comments to know exactly what is happening!

Does this code follow best practices that we have discussed?

Select the correct answer

Everything was clear?

Section 2. Chapter 2
some-alt