C++ unique_ptr member functions swap, release, reset

In this article, we are going to learn about the member functions of unique_ptr class template. These functions are very useful when we make use of the unique_ptr smart pointer in our programs and applications code.

There are many types of functions associated with unique_ptr pointer. These are:

  1. Modifiers
    1. release()
    2. reset()
    3. swap()
  2. Observers
    1.get()
    2.get_deleter()
  3. Default member functions
    1. Constructor
    2. Destructor
    3. Operator=

Note* = Move constructor, Move assignment operator , Copy constructor are NOT available.

let us now discuss each of these functions one by one and understand how they are used in action. First let’s Discuss the Observer functions associated with unique_ptr.

#include <memory>
using namespace std;

struct UniquePtrObj
{
    UniquePtrObj() { std::cout << "Constructor UniquePtrObj...\n"; }
    ~UniquePtrObj() { std::cout << "Desctructor ~UniquePtrObj...\n"; }
};
 
struct CustomDeleter
{
    void DeleteCustom() { std::cout << "Call deleter CustomDeleter::DeleteCustom()...\n"; }
    void operator()(UniquePtrObj* ptr) const
    {
        std::cout << "Call delete for UniquePtrObj object...\n";
        delete ptr;
		ptr = nullptr;
    }
};


int main()
{
    // Declaration of a unique_ptr of type int with a int raw pointer.
    unique_ptr<int> ptr(new int(63));
	
	if(ptr)
	{
		cout<< " This is a unique_ptr pointer of type int";
	}
	
	cout<< " This is raw memory address of unique_ptr pointer"<<ptr.get();
	
	std::unique_ptr<UniquePtrObj, CustomDeleter> ptr1(new UniquePtrObj(), CustomDeleter());
    CustomDeleter& deleter = ptr1.get_deleter();
    deleter.DeleteCustom();
    return 0;
}

Let us now discuss the Modifier functions associated with unique_ptr.

release() member function

unique_ptr owns the memory location that it points to. If we want to release that memory or the ownership of the unique_ptr over that memory then we use the release() function. Once we use the release() function on a unique_ptr ,the unique_ptr will no longer have the ownership rights over the memory which it initially points to. release() function returns the address of the memory which was pointed by the unique_ptr.

#include <memory>
using namespace std;

int main()
{
    // Declaration of a unique_ptr of type int with a int raw pointer.
    unique_ptr<int> ptr(new int(63));
	
	if(ptr)
	{
		cout<< " This is a unique_ptr pointer of type int";
	}
	
	// This line causes memory leak.
	cout<< " This is raw memory address of unique_ptr pointer"<<ptr.release();
	

	// Proper method to call release
	auto *newPtr = ptr.release();
	delete newPtr;
	
    return 0;
}

reset() member function

unique_ptr owns the memory location that it points to. If we want to release that memory or the ownership of the unique_ptr over that memory Or we want to release and reassign a new memory to a unique_ptr. There are two use case of reset() function:

  1. reset() function with no arguments is used to free the memory pointed by the unique_ptr. In this case ownership over the memory is lost.
  2. reset() function with arguments is used to free the memory pointed by the unique_ptr and reallocate the new memory passed as argument to the unique_ptr. In this case ownership on memory pointed is lost and new ownership is assigned for new memory address.
#include <memory>
using namespace std;

int main()
{
    // Declaration of a unique_ptr of type int with a int raw pointer.
    unique_ptr<int> ptr(new int(63));
	
	if(ptr)
	{
		cout<< " This is a unique_ptr pointer of type int";
	}
	
	
	cout<< " This reset/free the memory address of unique_ptr pointer"<<ptr.reset();
	
	if(ptr)
	{
		cout<< " Now ptr is pointing to nullptr";
	}
	
	unique_ptr<int> ptr1(new int(63));
	ptr1.reset(new int(16) ) ;
	

	
    return 0;
}

swap() member function

swap() function is another modifier function that is associated with unique_ptr class. This function is used to swap the memory associated with two unique_ptr pointers.

let us understand this with the help of below code example:

#include <memory>
using namespace std;

int main()
{
    // Declaration of a unique_ptr of type int with a int raw pointer.
    unique_ptr<int> ptr(new int(63));
	unique_ptr<int> ptr1(new int(16));
	if(ptr)
	{
		cout<< " This is a unique_ptr pointer of type int";
	}
	
	
	cout<< " This will swap the memory address of unique_ptr pointers"<<ptr.swap(ptr1);
	
	
    std::cout << "The swapped new Value at Ptr is: " << *ptr << '\n';
    std::cout << "The swapped new Value at Ptr is:" << *ptr1 << '\n';
	

    return 0;
}