C++ Medium · Lesson 1 · On the house

unique_ptr

One owner. Lesson 1 is free — on the house.

Lessons · C++ Medium · Lesson 1 of 10 · On the house

unique_ptr

One owner.

Illustration for the C++ course
Lesson 1 is on the house. The rest opens with the paper, the Daily, or a gift.

You already know Introductory and Easy. Medium assumes those habits and asks for sharper tools.

std::unique_ptr is an exclusive owning smart pointer: exactly one owner at a time. When the unique_ptr is destroyed, it deletes the owned object. #include . Prefer std::make_unique(...) to create one — exception-safe and clear. When the compiler or runtime complains, read the first error first — later errors are often fallout. Change one thing. Rebuild. That calm loop beats rewriting the whole file from memory. Ownership is the plot of modern C++. Ask who creates the object, who may use it, and who destroys it. If you can't answer, you're guessing — and guessing with pointers is how undefined behavior sneaks in. Before you build, predict what unique_ptr should do on the still's inputs. Prediction turns compiling into confirmation.

auto p = std::make_unique(3); allocates an int with value 3 and wraps it. Copying unique_ptr is deleted — you move it instead. That encodes uniqueness in the type system. Pass unique_ptr by value to transfer ownership; pass T& or T* (non-owning) to borrow. Ownership is the plot of modern C++. Ask who creates the object, who may use it, and who destroys it. If you can't answer, you're guessing — and guessing with pointers is how undefined behavior sneaks in. Templates are compile-time machinery. The payoff is reusable code; the cost is error messages that read like a novel. Start from a tiny example you can instantiate by hand. Before you build, predict what unique_ptr should do on the still's inputs. Prediction turns compiling into confirmation.

release() yields the raw pointer and gives up ownership (you become responsible). reset() deletes the current object (or replaces it). get() borrows the raw pointer without releasing ownership — don't delete what get() returns. Ownership is the plot of modern C++. Ask who creates the object, who may use it, and who destroys it. If you can't answer, you're guessing — and guessing with pointers is how undefined behavior sneaks in. Move is not magic copy — it transfers resources. After a move, treat the source as empty-but-valid unless you know better. That mental model prevents use-after-move bugs. Before you build, predict what unique_ptr should do on the still's inputs. Prediction turns compiling into confirmation.

unique_ptr is the default choice for heap ownership in modern C++. Shared ownership is rarer than people think — try unique first. Arrays have unique_ptr<T[]> specialization; prefer vector for most sequences anyway. Zero-based indexing is the tax you pay once and forever. When something looks one-off, count from zero on paper before you blame the standard library. Ownership is the plot of modern C++. Ask who creates the object, who may use it, and who destroys it. If you can't answer, you're guessing — and guessing with pointers is how undefined behavior sneaks in. Before you build, predict what unique_ptr should do on the still's inputs. Prediction turns compiling into confirmation.

Custom deleters exist for non-new resources (FILE*, handles). The still uses the default deleter for a single int. Ownership plot: who creates, who destroys — unique_ptr answers “this object destroys it.” Ownership is the plot of modern C++. Ask who creates the object, who may use it, and who destroys it. If you can't answer, you're guessing — and guessing with pointers is how undefined behavior sneaks in. Move is not magic copy — it transfers resources. After a move, treat the source as empty-but-valid unless you know better. That mental model prevents use-after-move bugs. Before you build, predict what unique_ptr should do on the still's inputs. Prediction turns compiling into confirmation.

Don't mix raw owning new with unique_ptr casually. Once it's in a unique_ptr, let the smart pointer finish the job. Double-delete and leaks are what we're ending. When the compiler or runtime complains, read the first error first — later errors are often fallout. Change one thing. Rebuild. That calm loop beats rewriting the whole file from memory. Zero-based indexing is the tax you pay once and forever. When something looks one-off, count from zero on paper before you blame the standard library. Before you build, predict what unique_ptr should do on the still's inputs. Prediction turns compiling into confirmation.

Put unique_ptr in a sentence you could teach a friend: what changes in the program, what stays the same, and what you expect when you build and run the still. Zero-based indexing is the tax you pay once and forever. When something looks one-off, count from zero on paper before you blame the standard library. Ownership is the plot of modern C++. Ask who creates the object, who may use it, and who destroys it. If you can't answer, you're guessing — and guessing with pointers is how undefined behavior sneaks in. Before you build, predict what unique_ptr should do on the still's inputs. Prediction turns compiling into confirmation.

up

#include <memory>
auto p = std::make_unique<int>(3);
Exclusive ownership via make_unique.

make_unique(3) heap-allocates an int initialized to 3 and returns a unique_ptr owning it. p is the sole owner. When p leaves scope, the int is deleted. No delete in your source — RAII via unique_ptr.

Pitfalls: copying unique_ptr (illegal); deleting get(); using release() and forgetting; shared_ptr by default out of habit; make_unique unavailable on ancient dialects — use C++14+. One owner, clear lifetime.

Quiz

What does unique_ptr represent?

Quiz

Why prefer make_unique?

Quiz

Can you copy a unique_ptr?

Quiz

What happens when a unique_ptr is destroyed?

Quiz

What does get() return?

Quiz

Which header provides unique_ptr?

Check

Match the still for: unique_ptr.

One owner is the default story. Next: shared_ptr — shared ownership, used sparingly.

Open-book: the answers are on this page. Pass every quiz and check (7) to mark the lesson done. This visit: 0/7.

unique_ptr · C++ Medium (free) — The Gold Standard