Interview Q&A · C++ · 10 questions in this topic

RAII & Smart Pointers — C++ Interview Q&A

All C++ topics

Deterministic resource management — RAII, unique/shared/weak pointers, cycles, and custom deleters. Part of Pointers, References & RAII · 213 questions in the C++ bank.

What is RAII and why is it the central C++ idiom?

Answer

RAII (Resource Acquisition Is Initialization) ties resource lifetime to object lifetime: the constructor acquires, the destructor releases. Because destructors run deterministically at scope exit — including during exception unwinding — resources (memory, files, locks, handles) are freed exactly once on every path.

std::string, containers, lock_guard and smart pointers are all RAII types; writing new RAII wrappers is how you make manual cleanup unnecessary.

Example

Example.cppcpp
#include <mutex>
void work(std::mutex& m) {
    std::lock_guard<std::mutex> g(m); // acquire
    // ... exceptions safe: g destructor unlocks on every path
}

💡 Tie RAII to exceptions explicitly — stack unwinding running destructors is why RAII beats finally-style cleanup.

When do you use `unique_ptr`, `shared_ptr` and `weak_ptr`?

Answer

unique_ptr is exclusive ownership with zero overhead — the default smart pointer. shared_ptr is shared ownership via atomic reference counting — use only when truly shared. weak_ptr is a non-owning observer of shared_ptr that lock()s to a temporary shared_ptr, breaking cycles and expressing caches/observers.

Default to unique_ptr; reach for shared_ptr only with proven shared lifetime, and weak_ptr for back-references and caches.

Key points

  1. 1unique_ptr — exclusive, movable, zero cost; factory: make_unique.
  2. 2shared_ptr — counted, thread-safe count, cyclic danger; factory: make_shared.
  3. 3weak_ptr — observes shared_ptr, lock() to use, breaks cycles.

Example

Example.cppcpp
#include <memory>
auto u = std::make_unique<int>(5); // exclusive
std::shared_ptr<int> s = std::make_shared<int>(6);
std::weak_ptr<int> w = s; // observes, owns nothing
if (auto tmp = w.lock()) { /* safe to use */ }

💡 Default-unique, shared-only-when-proven, weak-for-cycles is the ownership mantra.

How does `shared_ptr` reference counting work? What does it cost?

Answer

shared_ptr points at the object plus a control block holding strong and weak counts. Copying increments (atomically) the strong count; destruction decrements; at zero the object is destroyed, and at zero weak count the block itself is freed. make_shared fuses object and block into one allocation.

Costs: one extra allocation (without make_shared), atomic count traffic on every copy (visible in hot loops), and 16 bytes per shared_ptr (two pointers). Cycles need weak_ptr to break.

Example

Example.cppcpp
auto s1 = std::make_shared<int>(1); // one allocation
{
    auto s2 = s1; // strong count 2 (atomic inc)
} // s2 dies: count back to 1

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits