Smart Pointers: Smart Pointers using reset() Method

What is it?

C++ Smart Pointers reset() method is used to replace the managed object with a new one. If the Smart Pointer object is the only one that has ownership of the object, before assigning the new pointer, it deletes the old one and frees up the memory. This method ensures that there are no memory leaks by taking care of deallocating memory that is no longer in use.


Syntax

The syntax of using the reset() method is:

void reset (pointer p = pointer()) noexcept;

Where,

  • pointer_name: It is the name of the smart pointer.
  • p: Is the new pointer to be managed.



How to use it

To use the reset() method, follow the below steps:

  1. Create a Smart Pointer and initialize it with a new object.
  2. Use the reset() method to replace the managed object with a new one.



Program code snippet example

#include <iostream>
#include <memory>

using namespace std;

int main() {

    // Creating a unique_ptr and initializing it with a new object.
    unique_ptr<int> myPtr(new int(10));

    cout << "Old value: " << *myPtr << endl;

    // Replacing the managed object with a new one.
    myPtr.reset(new int(20));

    cout << "New value: " << *myPtr << endl;

    return 0;
}

Output:

Old value: 10
New value: 20



Important to know

  • The reset() method provides an efficient way to replace the managed object without worrying about deallocating the memory of the old object. It makes sure that there are no memory leaks.
  • The reset() method can also be used to release the ownership of the managed object by calling it without passing any arguments.
  • If the Smart Pointer object was the only one managing the object, the object gets deleted when reset() is called. Hence, any other raw pointers pointing to this object become dangling pointers.



Best practices

When using the reset() method, it is important to keep the following best practices in mind:

  1. Use reset() to replace the managed object: The reset() method provides an efficient way to replace the managed object without worrying about deallocating the memory of the old object. Hence, prefer using reset() instead of manual memory management.

  2. Be aware of the effect of reset() on shared pointers: If the reset() method is called on a shared pointer, the reference count of the object is decreased by one. If the reference count becomes zero, the object is deleted.

  3. Release ownership when needed: The reset() method can also be used to release the ownership of the managed object. This can be useful in situations where you want to hand over the ownership to another smart pointer or if you want to make sure that the object gets deleted at a certain point in the code.

  4. Avoid accessing the old object after reset(): After calling reset(), the old object gets deleted if no other smart pointer is managing it. Therefore, avoid accessing the old object after calling reset() as it leads to undefined behavior.




Call and output the method reset() for the std::unique_ptr boss object.