Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What Are Unique Pointers? | Unique Pointers
C++ Smart Pointers
course content

Course Content

C++ Smart Pointers

C++ Smart Pointers

1. Introduction to Smart Pointers
2. Unique Pointers
3. Shared Pointers
4. Weak Pointers
5. References
6. Advanced topics

bookWhat Are Unique Pointers?

Unique pointers are arguably the simplest of the smart pointers in C++. As the name suggests, a unique pointer, defined as std::unique_ptr, allows the allocated resource (the pointed object) to have only one owner.

This means that unique pointers don’t have to perform any reference counting, which tracks the number of pointers sharing the ownership of a resource.

Enforcing this one-to-one relationship leads to lesser memory overhead, boosting the overall performance of C++ applications.

Copying not allowed

A unique pointer contains a raw pointer to the allocated resource/object. It’s not possible to copy a unique pointer, or pass it by value to a function.

Moving a unique pointer

However, you can transfer ownership of the object pointed to by a std::unique_ptr, using the std::move function.

When you use std::move, it essentially "moves" the ownership of the object from uniquePtr1 to uniquePtr2. This means that after this line of code, uniquePtr1 will no longer own the object, and uniquePtr2 will be the new owner.

More things to know

When a unique pointer goes out of scope (e.g. a function exits), or is explicitly reset, the allocated memory is <strong class="go98639658">automatically released</strong>.

Even though it’s not possible to pass a unique pointer to an external class, you can extract and pass the raw pointer using the get() method.

Note

This approach is generally not recommended, as it transfers the responsibility of memory management to the external class. A better alternative, in most cases, is either to use a shared pointer, or to consider transferring ownership of the unique pointer.

Task

  • Complete the following code in a way that the ownership of the dynamically allocated integer (with value 42) is safely transferred from ptr1 to ptr2.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
toggle bottom row

bookWhat Are Unique Pointers?

Unique pointers are arguably the simplest of the smart pointers in C++. As the name suggests, a unique pointer, defined as std::unique_ptr, allows the allocated resource (the pointed object) to have only one owner.

This means that unique pointers don’t have to perform any reference counting, which tracks the number of pointers sharing the ownership of a resource.

Enforcing this one-to-one relationship leads to lesser memory overhead, boosting the overall performance of C++ applications.

Copying not allowed

A unique pointer contains a raw pointer to the allocated resource/object. It’s not possible to copy a unique pointer, or pass it by value to a function.

Moving a unique pointer

However, you can transfer ownership of the object pointed to by a std::unique_ptr, using the std::move function.

When you use std::move, it essentially "moves" the ownership of the object from uniquePtr1 to uniquePtr2. This means that after this line of code, uniquePtr1 will no longer own the object, and uniquePtr2 will be the new owner.

More things to know

When a unique pointer goes out of scope (e.g. a function exits), or is explicitly reset, the allocated memory is <strong class="go98639658">automatically released</strong>.

Even though it’s not possible to pass a unique pointer to an external class, you can extract and pass the raw pointer using the get() method.

Note

This approach is generally not recommended, as it transfers the responsibility of memory management to the external class. A better alternative, in most cases, is either to use a shared pointer, or to consider transferring ownership of the unique pointer.

Task

  • Complete the following code in a way that the ownership of the dynamically allocated integer (with value 42) is safely transferred from ptr1 to ptr2.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1
toggle bottom row

bookWhat Are Unique Pointers?

Unique pointers are arguably the simplest of the smart pointers in C++. As the name suggests, a unique pointer, defined as std::unique_ptr, allows the allocated resource (the pointed object) to have only one owner.

This means that unique pointers don’t have to perform any reference counting, which tracks the number of pointers sharing the ownership of a resource.

Enforcing this one-to-one relationship leads to lesser memory overhead, boosting the overall performance of C++ applications.

Copying not allowed

A unique pointer contains a raw pointer to the allocated resource/object. It’s not possible to copy a unique pointer, or pass it by value to a function.

Moving a unique pointer

However, you can transfer ownership of the object pointed to by a std::unique_ptr, using the std::move function.

When you use std::move, it essentially "moves" the ownership of the object from uniquePtr1 to uniquePtr2. This means that after this line of code, uniquePtr1 will no longer own the object, and uniquePtr2 will be the new owner.

More things to know

When a unique pointer goes out of scope (e.g. a function exits), or is explicitly reset, the allocated memory is <strong class="go98639658">automatically released</strong>.

Even though it’s not possible to pass a unique pointer to an external class, you can extract and pass the raw pointer using the get() method.

Note

This approach is generally not recommended, as it transfers the responsibility of memory management to the external class. A better alternative, in most cases, is either to use a shared pointer, or to consider transferring ownership of the unique pointer.

Task

  • Complete the following code in a way that the ownership of the dynamically allocated integer (with value 42) is safely transferred from ptr1 to ptr2.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Unique pointers are arguably the simplest of the smart pointers in C++. As the name suggests, a unique pointer, defined as std::unique_ptr, allows the allocated resource (the pointed object) to have only one owner.

This means that unique pointers don’t have to perform any reference counting, which tracks the number of pointers sharing the ownership of a resource.

Enforcing this one-to-one relationship leads to lesser memory overhead, boosting the overall performance of C++ applications.

Copying not allowed

A unique pointer contains a raw pointer to the allocated resource/object. It’s not possible to copy a unique pointer, or pass it by value to a function.

Moving a unique pointer

However, you can transfer ownership of the object pointed to by a std::unique_ptr, using the std::move function.

When you use std::move, it essentially "moves" the ownership of the object from uniquePtr1 to uniquePtr2. This means that after this line of code, uniquePtr1 will no longer own the object, and uniquePtr2 will be the new owner.

More things to know

When a unique pointer goes out of scope (e.g. a function exits), or is explicitly reset, the allocated memory is <strong class="go98639658">automatically released</strong>.

Even though it’s not possible to pass a unique pointer to an external class, you can extract and pass the raw pointer using the get() method.

Note

This approach is generally not recommended, as it transfers the responsibility of memory management to the external class. A better alternative, in most cases, is either to use a shared pointer, or to consider transferring ownership of the unique pointer.

Task

  • Complete the following code in a way that the ownership of the dynamically allocated integer (with value 42) is safely transferred from ptr1 to ptr2.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt