Caffeinated Coder
Book an appointment
Lesson 18 · Modern C++

Smart pointers: unique_ptr and shared_ptr

A smart pointer is a small object that owns heap memory and deletes it in its destructor, so you never write delete. Follow a twelve-line program as std::make_unique creates an int with exactly one owner, std::move hands that ownership over, std::make_shared creates an int with a counted control block, copies raise and lower the use count, and at the end of main every owner frees what it owns, in reverse order, with no leak.

14 min11 steps5 questionsAdvanced
Fig 18 · Interactive
Loading figure…
or ← →

Quiz Unlocks after the last step.