C++ memory reset() Function
Example
Make a unique_ptr point to a new value:
unique_ptr<int> ptr(new int(25));
ptr.reset(new int(50));
cout << *ptr;
Definition and Usage
The reset() function frees the memory that a smart pointer owns. If you give it a new pointer, the smart pointer becomes the owner of the new memory.
Without a parameter, reset() makes the smart pointer empty. For a shared_ptr, the memory is only freed if it was the last owner.
The reset function is defined in the <memory> header file.
Syntax
ptr.reset();
ptr.reset(newPointer);
ptr is a unique_ptr or a shared_ptr.
Parameter Values
| Parameter | Description |
|---|---|
| newPointer | Optional. A pointer to new memory, like new int(50). |
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about smart pointers in our Smart Pointers Tutorial.