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

Зміст курсу

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

book
What 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 automatically released.

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.

Завдання
test

Swipe to show code editor

  • 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 desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
toggle bottom row

book
What 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 automatically released.

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.

Завдання
test

Swipe to show code editor

  • 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 desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt