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

Contenido del Curso

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 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.

Tarea
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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 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 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.

Tarea
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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 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 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.

Tarea
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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

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.

Tarea
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 desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 2. Capítulo 1
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt