KatInterview

Build a Reference-Counted Shared Ownership Pointer in C++

Data Structure DesignModel answer
MediumModel answerData Structure Designc++memory-managementconcurrency

Last updated

Implement a thread-safe shared ownership smart pointer class template from scratch, covering copy/move semantics, atomic reference counting, and correct destruction.

Shared Ownership Smart Pointer

Modern C++ programs often need several parts of a system to jointly own a heap-allocated object — none of them individually responsible for freeing it, yet the object must be freed exactly once when the last owner is done. Your task is to build that mechanism yourself.

Implement a class template SharedPtr<T> that provides shared ownership semantics for a single heap-allocated object. The object must stay alive as long as at least one SharedPtr refers to it, and must be destroyed the moment the last one is released — no sooner, no later.

Required interface

template <typename T>
class SharedPtr {
public:
    SharedPtr() noexcept;                        // empty; owns nothing
    explicit SharedPtr(T* p);                    // takes ownership of p (new-allocated)
    SharedPtr(const SharedPtr& other);           // shares ownership
    SharedPtr(SharedPtr&& other) noexcept;       // transfers ownership; other becomes empty
    SharedPtr& operator=(const SharedPtr& other);
    SharedPtr& operator=(SharedPtr&& other) noexcept;
    ~SharedPtr();

    void reset(T* p = nullptr);                  // drop current object; optionally own p
    T*          get()        const noexcept;
    T&          operator*()  const;
    T*          operator->() const noexcept;
    explicit    operator bool() const noexcept;
    std::size_t use_count()  const noexcept;     // 0 when empty
};

Constraints

  • C++17 or later. You may not use std::shared_ptr, std::unique_ptr, or std::weak_ptr.
  • The pointer manages single objects allocated with new, not arrays.
  • An empty SharedPtr — including one constructed from nullptr — must report use_count() == 0.
  • Reference count updates must be safe when copies of the same SharedPtr are created or destroyed concurrently on different threads.

What you should be ready to discuss

  • Where the reference count is stored and who is responsible for freeing it.
  • The exact memory ordering used for atomic count operations and why you chose it.
  • Which operations are thread-safe and which are not.
  • What happens on self-assignment and why it matters.
  • How the implementation would change to support a non-owning (weak) reference, base-class conversion, or a custom deleter.

Hints

Hint 1

You need a single reference count that all copies of the same SharedPtr can see and modify. Think about where to allocate it and who frees it — it has a different lifetime than the managed object itself.

Hint 2

Atomic increment and decrement avoid data races on the count, but memory ordering matters: what must be visible to the thread that observes the count drop to zero before it calls delete?

Hint 3

Write copy assignment as: increment the incoming count first, then release the old object. That one ordering makes both self-assignment and exception safety fall out naturally without a special-case check.