Shared Pointer

Normal 3.5 Exclusive 15s 512 MB
BasicsMemory Management
Part 1

Shared ownership

Open

Implement SharedPtr<T>: the constructors, both assignments, the destructor, reset and swap. The accessors and use_count are written for you.

The tests read use_count() from every owner after every change, delete through the last owner only, and count allocations: one per SharedPtr(ptr), none for a copy, a move or an empty pointer. They abort on a double destroy, audit every allocation, and overwrite freed memory before it is reused.

Some assignments alias: a SharedPtr is assigned to itself, both plainly and with std::move, while it is the only owner of its object, and head = head->next pops a chain whose head is the only owner of the node that holds next.

Implement SharedPtr<T>, a reference-counted smart pointer that lets several owners share one heap object, and then WeakPtr<T>, which observes that object without owning it. Together they mirror the core of std::shared_ptr<T> and std::weak_ptr<T>.

Several SharedPtr<T> objects can own the same heap object. They agree on how many owners it has through a count they all share, and the last owner to let go deletes the object. A WeakPtr<T> (Part 2) watches an object without owning it: it can tell whether the object still exists and can turn itself into a new owner with lock(), but it never keeps the object alive.

C++
template <class T>
class SharedPtr {
public:
    SharedPtr() noexcept;                              // owns nothing
    explicit SharedPtr(T* ptr);                        // first owner of ptr (never null in the tests)
    SharedPtr(const SharedPtr& other) noexcept;        // one more owner
    SharedPtr(SharedPtr&& other) noexcept;             // takes other's share, other becomes empty
    SharedPtr& operator=(const SharedPtr& other) noexcept;
    SharedPtr& operator=(SharedPtr&& other) noexcept;
    ~SharedPtr();

    T* get() const noexcept;
    T& operator*() const noexcept;
    T* operator->() const noexcept;
    explicit operator bool() const noexcept;
    long use_count() const noexcept;                   // owners of the object, 0 when empty

    void reset() noexcept;                             // let go, become empty
    void reset(T* ptr);                                // let go, become the first owner of ptr
    void swap(SharedPtr& other) noexcept;
};

template <class T>
class WeakPtr {                                        // Part 2
public:
    WeakPtr() noexcept;                                // observes nothing
    WeakPtr(const SharedPtr<T>& shared) noexcept;      // observes shared's object
    WeakPtr(const WeakPtr& other) noexcept;
    WeakPtr(WeakPtr&& other) noexcept;                 // other becomes empty
    WeakPtr& operator=(const WeakPtr& other) noexcept;
    WeakPtr& operator=(WeakPtr&& other) noexcept;
    WeakPtr& operator=(const SharedPtr<T>& shared) noexcept;
    ~WeakPtr();

    long use_count() const noexcept;                   // owners of the object, 0 once it is gone
    bool expired() const noexcept;                     // use_count() == 0
    SharedPtr<T> lock() const noexcept;                // a new owner, or an empty SharedPtr if expired
    void reset() noexcept;                             // observe nothing
};

The harness declares template <class T> class SharedPtr; and template <class T> class WeakPtr; above your code, so define both with exactly that template header, at namespace scope.

  • use_count() is the number of SharedPtr objects that currently own the object. WeakPtrs are never counted in it. An empty SharedPtr reports 0.
  • The object is deleted, with delete, exactly when its last owner is destroyed, reset, or assigned something else. Not before, and not later.
  • After a = b, a shares b's object, and the object a owned before loses one owner. a = a changes nothing, and neither does assigning a SharedPtr that already shares a's object. a = std::move(a) leaves a owning its object.
  • A move transfers a share: the destination owns, the source is empty, and use_count() does not change.
  • Copying, moving, assigning and destroying a SharedPtr never allocate. SharedPtr(ptr) and reset(ptr) allocate exactly once, for whatever the owners share. An empty SharedPtr never allocates.
  • A WeakPtr made from an empty SharedPtr is expired. Making, copying and destroying a WeakPtr never allocate.
  • Single-threaded: the counts are plain integers, not atomics.

The tests count constructions and destructions of their element types, abort on a double destroy, and audit every allocation a scenario makes, so whatever your SharedPtr allocates has to be freed exactly once too. Freed memory is overwritten before it is reused, so reading through a pointer to something already freed fails a check rather than reading stale values.

The harness defines unique_ptr, shared_ptr, weak_ptr, auto_ptr, make_unique, make_shared and enable_shared_from_this away, so none of them may appear in a submission, member and variable names included. Do not write main; the harness owns it. The standard headers are already included.

C++
SharedPtr<Widget> a(new Widget(1));     // a.use_count() == 1
SharedPtr<Widget> b = a;                // both report 2
a.reset();                              // b.use_count() == 1, Widget 1 alive

WeakPtr<Widget> w(b);                   // Part 2: b.use_count() is still 1
SharedPtr<Widget> c = w.lock();         // c and b own Widget 1, use_count() == 2
b.reset();
c.reset();                              // Widget 1 deleted here
w.expired();                            // true
w.lock();                               // an empty SharedPtr

None. The judge compiles your class into a hidden harness that drives it directly and reports one row per scenario.

None. Your code prints nothing; the harness owns the verdict.

  • operator* and operator-> on an empty SharedPtr are undefined behaviour. The tests never do it.

  • No custom deleters, no aliasing constructor, no make_shared and no conversions between SharedPtr<Derived> and SharedPtr<Base>.

Accepted 9/10
Acceptance 90%
Loading editor...
Input
shared-ownership
Expected output
PASS