Smart Pointers

Smart pointers are C++ objects that wrap a raw pointer and automatically free the memory it owns when it is no longer needed, so the programmer does not call delete by hand. The standard library provides unique_ptr for exclusive ownership, shared_ptr for reference-counted shared ownership, and weak_ptr, a non-owning reference used to break reference cycles.

3 min read

Smart pointers are C++ objects that wrap a raw pointer and automatically manage the memory it points to, freeing it exactly when it's no longer needed. They exist specifically to solve the two most common categories of bugs in manual memory management: forgetting to free memory (a leak) and freeing it more than once or too early (undefined behavior).

How they work

A smart pointer's destructor automatically calls delete on the memory it owns, and since C++ guarantees a local object's destructor runs when it goes out of scope, memory owned by a smart pointer gets freed automatically, deterministically, without the programmer having to remember to do it manually. The C++ standard library provides three main kinds:

unique_ptr: owns the memory exclusively, no two unique_ptrs can point to the same object at once. Ownership can be transferred (moved) but never shared.

shared_ptr: multiple shared_ptrs can point to the same object, using an internal reference count, the memory is only freed once the last shared_ptr pointing to it is destroyed.

weak_ptr: a non-owning reference to an object managed by a shared_ptr, used specifically to break reference cycles that would otherwise prevent shared_ptr's reference count from ever reaching zero.

A worked example

#include <iostream>
#include <memory>
using namespace std;

class Resource {
public:
    Resource()  { cout << "Resource acquired" << endl; }
    ~Resource() { cout << "Resource released" << endl; }
    void use()  { cout << "Using resource" << endl; }
};

void withRawPointer() {
    Resource* r = new Resource();
    r->use();
    // if an exception were thrown here, delete r would never run, a real leak
    delete r;
}

void withSmartPointer() {
    unique_ptr<Resource> r = make_unique<Resource>();
    r->use();
    // no delete needed, the Resource is automatically released when r goes out of scope,
    // even if an exception is thrown in between
}

int main() {
    withSmartPointer();
    return 0;
}

Output:

Resource acquired
Using resource
Resource released

With unique_ptr, the Resource is released automatically the instant r goes out of scope at the end of withSmartPointer(), with zero chance of forgetting to free it, even if an exception had been thrown partway through the function.

Where this matters in real systems

Modern C++ style guides (including the ones followed at major companies with large C++ codebases, like Google's) explicitly discourage raw new/delete in application code in favor of smart pointers, specifically because manual memory management bugs (leaks, double-frees, use-after-free) are historically one of the largest sources of real security vulnerabilities and crashes in C++ software. shared_ptr is the standard tool any time an object's ownership genuinely needs to be shared across multiple parts of a system, for instance a cached resource that many different components hold a reference to simultaneously.

What interviewers actually ask about smart pointers

  • "What's the difference between unique_ptr and shared_ptr?" unique_ptr allows exactly one owner at a time (ownership can be moved but not copied); shared_ptr allows multiple simultaneous owners via reference counting.
  • "When would you use weak_ptr?" To reference an object managed by a shared_ptr without contributing to its reference count, most commonly to break a reference cycle (two objects shared_ptr-owning each other) that would otherwise leak memory forever.
  • "What problem do smart pointers actually solve?" Manual memory management errors, specifically memory leaks (forgetting to free) and dangling/double-free bugs (freeing incorrectly), by tying deallocation to an object's deterministic destructor instead of a manual delete call.
  • "Is there overhead to using shared_ptr compared to a raw pointer?" Yes, shared_ptr maintains an atomic reference count and control block, which carries real (though usually small) performance overhead compared to a raw pointer or unique_ptr.

Key takeaways

  • Smart pointers automatically free memory when it's no longer needed, tied to deterministic object destruction.
  • unique_ptr allows exactly one owner; shared_ptr allows multiple owners via reference counting.
  • weak_ptr is a non-owning reference used to break reference cycles between shared_ptrs.
  • Modern C++ style strongly favors smart pointers over raw new/delete to avoid leaks and use-after-free bugs.
All 72 OOP interview questions

More in Misc

Log in to save which topics you've read across every subject.

Frequently asked questions

What's the difference between `unique_ptr` and `shared_ptr`?

`unique_ptr` allows exactly one owner at a time (ownership can be moved but not copied); `shared_ptr` allows multiple simultaneous owners via reference counting.

When would you use `weak_ptr`?

To reference an object managed by a `shared_ptr` without contributing to its reference count, most commonly to break a reference cycle (two objects `shared_ptr`-owning each other) that would otherwise leak memory forever.

What problem do smart pointers actually solve?

Manual memory management errors, specifically memory leaks (forgetting to free) and dangling/double-free bugs (freeing incorrectly), by tying deallocation to an object's deterministic destructor instead of a manual `delete` call.

Is there overhead to using `shared_ptr` compared to a raw pointer?

Yes, `shared_ptr` maintains an atomic reference count and control block, which carries real (though usually small) performance overhead compared to a raw pointer or `unique_ptr`.