← Back to C++ Course | Chapter 14: Memory Management | Lesson 5 of 6

C++ unique_ptr

What is unique_ptr?

std::unique_ptr, introduced in C++11, wraps a raw pointer and guarantees that exactly one owner exists for the object at any time. When the unique_ptr variable goes out of scope, its destructor automatically calls delete on the managed object, so you never have to remember to free it manually.

Example: What is unique_ptr?

cpp
#include <iostream>
#include <memory>

int main() {
	std::unique_ptr<int> ptr(new int(42));
	std::cout << *ptr << std::endl;
	return 0;
}

No Copying, Only Moving

Because a unique_ptr represents exclusive ownership, its copy constructor is deleted -- attempting to copy one is a compile error. To hand ownership to another unique_ptr, you must explicitly std::move it, which transfers the underlying pointer and leaves the original empty rather than duplicating access.

Example: No Copying, Only Moving

cpp
#include <iostream>
#include <memory>

int main() {
	std::unique_ptr<int> ptr1(new int(5));
	std::unique_ptr<int> ptr2 = std::move(ptr1);
	std::cout << *ptr2 << std::endl;
	return 0;
}

reset() and release()

Calling reset() immediately deletes the currently owned object and optionally starts managing a new one you pass in. Calling release() instead hands back the raw pointer to you and forgets about it entirely, so you become responsible for deleting it manually -- useful when interfacing with older APIs that expect raw pointers.

Example: reset() and release()

cpp
#include <iostream>
#include <memory>

int main() {
	std::unique_ptr<int> ptr(new int(5));
	ptr.reset(new int(10));
	std::cout << *ptr << std::endl;
	return 0;
}

unique_ptr with Arrays

For dynamically allocated arrays, declare unique_ptr<T[]> rather than unique_ptr<T> -- this specialization overloads operator[] for element access and automatically calls delete[] instead of delete when it goes out of scope, matching how the array was originally allocated.

Example: unique_ptr with Arrays

cpp
#include <iostream>
#include <memory>

int main() {
	std::unique_ptr<int[]> arr(new int[3]{1, 2, 3});
	std::cout << arr[1] << std::endl;
	return 0;
}

make_unique (C++14)

std::make_unique<T>(args) is the recommended way to create a unique_ptr, since it constructs the object and wraps it in one expression -- avoiding a bare new that could leak memory if an exception were thrown between allocation and wrapping. It was added in C++14 to match make_shared, which existed since C++11.

Example: make_unique (C++14)

cpp
#include <iostream>
#include <memory>

int main() {
	auto ptr = std::make_unique<int>(99);
	std::cout << *ptr << std::endl;
	return 0;
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.