KatInterview

Build a Reference-Counted Pointer from Scratch in C++

Data Structure DesignModel answer
HardModel answerData Structure DesignC++RAIImemory management

Last updated

Implement a class template SharedPtr<T> that manages heap object lifetime through reference counting, supporting copy, move, reset, and safe self-assignment.

Build a Reference-Counted Pointer from Scratch in C++

The C++ standard library's std::shared_ptr is one of the most important tools for safe heap memory management. Your task is to build a simplified version of it from first principles.

What to implement

Write a class template SharedPtr<T> that takes ownership of a heap-allocated T* and ensures the object is destroyed exactly once — when the last SharedPtr that owns it goes out of scope or releases it.

Your implementation must support:

  • Default constructor — creates a null, non-owning pointer
  • Constructor from T* — takes ownership of the given raw pointer
  • Destructor — releases ownership; deletes the object if this was the last owner
  • Copy constructor and copy assignment — both objects end up sharing ownership; the reference count increases by one
  • Move constructor and move assignment — ownership transfers to the new object; the source becomes null
  • T& operator*() and T* operator->() — dereference the managed object
  • T* get() const — returns the raw pointer without transferring ownership
  • size_t use_count() const — returns how many SharedPtr instances currently own the object
  • void reset(T* p = nullptr) — releases the current object and optionally takes ownership of a new one

Rules and constraints

  • The managed object must be deleted exactly once. No double-frees, no leaks.
  • Self-assignment (a = a) and self-move (a = std::move(a)) must both be handled correctly.
  • use_count() on a null SharedPtr must return 0.
  • You do not need to support thread-safety unless you want to — but note the trade-off.
  • Constructing two independent SharedPtrs from the same raw pointer is considered misuse; you do not need to detect it.

What a complete answer looks like

Provide:

  1. The full implementation of SharedPtr<T>, with brief comments explaining non-obvious decisions
  2. A walkthrough of why the implementation is correct — specifically, what invariant it maintains and how each operation preserves it
  3. Complexity analysis for each operation
  4. A discussion of the key edge cases and how your code handles them

Hints

Hint 1

The reference count must live on the heap and be shared by all copies — if it lived inside the SharedPtr object itself, copies could not see each other's changes. Allocate it alongside or separately from the managed object.

Hint 2

Copy-and-swap is the cleanest way to implement both copy and move assignment. Construct a temporary from the source (which updates the count correctly), swap it with *this, then let the temporary's destructor handle releasing the old state. Self-assignment becomes a non-issue because the count reaches 2 before anything is deleted.

Hint 3

In the constructor that takes a raw T*, if allocating the control block throws, you must delete the raw pointer before rethrowing — otherwise the object the caller handed you leaks with no way to recover it.