std::unique_ptr is a smart pointer type provided by the C++ Standard Library for managing dynamic memory allocation. It is used to wrap a raw pointer and allocate or deallocate memory on the heap. The unique_ptr ensures that the memory pointed to by its stored pointer is reclaimed when the unique_ptr is destroyed or reset.
Code Example:
#include #include
int main() { std::unique_ptr myPointer = std::make_unique(42); std::cout << "Value of the integer pointed to by myPointer: " << *myPointer << std::endl; // Output: Value of the integer pointed to by myPointer: 42
// The memory pointed to by myPointer is automatically deallocated when it goes out of scope }
In this example, we create a unique_ptr to an integer and dynamically allocate memory using make_unique. We then dereference the unique_ptr to obtain the value of the integer.
The std::unique_ptr type can be found in the header of the C++ Standard Library.
C++ (Cpp) unique_ptr - 30 examples found. These are the top rated real world C++ (Cpp) examples of std::unique_ptr extracted from open source projects. You can rate examples to help us improve the quality of examples.