0Pricing
C++ Academy · Lesson

unique_ptr Exclusive Ownership

Express single-owner semantics with unique_ptr and move-only types.

unique_ptr Exclusive Ownership is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Single-Owner Smart Pointer

std::unique_ptr<T> owns one heap object. When the unique_ptr is destroyed, it deletes the object. Cannot be copied — only moved.

Creating a unique_ptr

Use std::make_unique (C++14) — exception safe and concise.

#include <memory>
auto w = std::make_unique<Widget>();         // default-constructed
auto p = std::make_unique<Point>(3, 4);       // forwarded args

Using It Like a Pointer

unique_ptr supports the usual pointer operations.

auto p = std::make_unique<Point>(3, 4);
std::cout << p->x;        // arrow access
std::cout << (*p).y;      // dereference
if (p) std::cout << "non-null";

Cannot Copy — Can Move

Copying would mean two owners — that violates "unique." Move transfers ownership.

std::unique_ptr<int> a = std::make_unique<int>(1);
std::unique_ptr<int> b = std::move(a);
// a is now empty, b owns the int

Passing to Functions

By value (with std::move): transfer ownership. By reference: borrow without owning.

void consume(std::unique_ptr<Widget> w);   // takes ownership
void inspect(const Widget& w);              // borrows

auto w = std::make_unique<Widget>();
consume(std::move(w));
// w is now empty
inspect(*w);                                // (after re-creating)

Returning from Functions

A function that allocates and returns ownership should return std::unique_ptr.

std::unique_ptr<Widget> create() {
    return std::make_unique<Widget>();
}

Resetting and Releasing

reset() deletes the current object and (optionally) takes ownership of a new one. release() gives up ownership and returns the raw pointer.

auto p = std::make_unique<int>(10);
p.reset(new int(20));    // delete the old, own the new
int* raw = p.release();   // p is now empty
delete raw;               // caller must delete

get(): Borrow the Raw Pointer

get() returns the raw pointer without giving up ownership. Use for non-owning interop.

auto p = std::make_unique<int>(42);
legacy_api(p.get());      // p still owns it

unique_ptr to Arrays

For arrays use unique_ptr<T[]>. Indexed access with []; delete[] is called on destruction.

auto arr = std::make_unique<int[]>(10);
arr[0] = 99;
std::cout << arr[0];

Custom Deleters

Provide a custom deleter as a template parameter when the resource is not raw memory (file handles, OS resources).

auto closer = [](FILE* f) { if (f) std::fclose(f); };
std::unique_ptr<FILE, decltype(closer)> file(std::fopen("a.txt","r"), closer);

Performance

With the default deleter, unique_ptr is the same size and speed as a raw pointer — zero overhead.

Quick Check

Why does std::unique_ptr forbid copying?

Recap

std::unique_ptr is the default smart pointer: zero overhead, exclusive ownership, movable but not copyable. Prefer std::make_unique for construction.

Frequently asked questions

Is the “unique_ptr Exclusive Ownership” lesson free?

Yes — the full text of “unique_ptr Exclusive Ownership” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “unique_ptr Exclusive Ownership”?

Express single-owner semantics with unique_ptr and move-only types. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “unique_ptr Exclusive Ownership” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Smart Pointers Replace Raw new delete
  2. unique_ptr Exclusive Ownership
  3. shared_ptr and weak_ptr Shared Ownership
  4. Custom Deleters and Make Functions
← Back to C++ Academy