Contenido del Curso
C++ Smart Pointers
C++ Smart Pointers
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.
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
toptr2
.
¡Gracias por tus comentarios!
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.
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
toptr2
.
¡Gracias por tus comentarios!