C++ memory release() Function
Example
Take the memory out of a unique_ptr:
unique_ptr<int> ptr(new int(25));
int* raw = ptr.release();
cout << *raw << "\n";
cout << (ptr == nullptr ? "ptr is empty" : "ptr is not empty");
delete raw;
Definition and Usage
The release() function returns the normal pointer that a unique_ptr holds, and makes the unique_ptr empty. The memory is not freed.
After release(), you are the owner of the memory, so you must free it with delete yourself.
The release function is defined in the <memory> header file.
Tip: If you want to free the memory, use reset() instead.
Syntax
uniquePtr.release();
Only a unique_ptr has this function.
Parameter Values
None.
Technical Details
| Returns: | A normal pointer to the memory, or nullptr if the unique_ptr was empty. |
|---|
Related Pages
Read more about smart pointers in our Smart Pointers Tutorial.