Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

UniquePtr with a Custom Deleter

Prerequisites: RAII: Cleanup That Happens Automatically, Raw Pointers, unique_ptr, shared_ptr, and weak_ptr, Move Semantics Without Magic, Object Lifetime: When C++ Objects Begin and End

A feed handler allocates one OrderBook per symbol on the heap and hands each book between components during the day. Exactly one component may free a given book, exactly once, whichever way control leaves the code that holds it.

std::unique_ptr puts that rule into a type. Build a smaller version of it.

What to implement

  • Write the class template UniquePtr<T, Deleter> at global scope. You write only the class.
  • A hidden driver instantiates it as UniquePtr<Tracked>, runs it through a list of commands, and prints what happened. Tracked is a driver type that reports its own construction and destruction.
  • The editor already declares every member the driver calls, plus DefaultDelete<T>, a deleter whose operator() runs delete p.
  • <utility> is already included, so std::move and std::exchange are available. #include any other header you need at the top of your code.
  • On stdin, the first line is the number of commands, and each remaining line is one command.
  • There are two parts. One solution must pass all ten tests.

The class

A UniquePtr is either empty (it holds nullptr) or owns one object. Destroying the owned object means calling the stored deleter on its pointer, deleter_(ptr_), never delete directly: Part 2 replaces the deleter, and the same code must keep working.

Member (Part 1)ContractCalled by
UniquePtr()Empty.make s
explicit UniquePtr(T* p)Owns p.make s id
UniquePtr(const UniquePtr&) = delete and operator=(const UniquePtr&) = deleteCopying does not compile.compile-time check
UniquePtr(UniquePtr&& other) noexceptTakes other's pointer and leaves other empty.move d s into an absent d
UniquePtr& operator=(UniquePtr&& other) noexceptDestroys the object *this owned, if any, takes other's pointer, and leaves other empty. Assigning a UniquePtr to itself changes nothing.move d s into a present d
~UniquePtr()Destroys the owned object, if any.drop s, end of input
T* get() constThe stored pointer, or nullptr.get s, deref s, watch s
T* release()Returns the pointer and leaves *this empty, destroying nothing. The caller now owns the object.release s
void reset(T* p = nullptr)Stores p first, then destroys the previously owned object, if any.reset s, reset s id
T& operator*() const and T* operator->() constThe owned object. Never called on an empty pointer.deref s
explicit operator bool() consttrue when not empty.bool s
  • reset(p) with p == get() and p != nullptr is outside the contract, and no test does it.
  • The driver queries slots through const references, so get, operator*, operator->, and operator bool must be const members.
  • The driver checks at compile time that UniquePtr<Tracked> cannot be copied, that both move operations are noexcept, and that the T* constructor and operator bool are explicit. A failed check is a compile error in driver.cpp (the hidden driver) whose message names the rule.

Commands

  • Each command is one line. Slots are named by one lowercase letter.
  • A slot is either absent or holds one UniquePtr, which may be empty.
  • Ids are positive integers, each used for one Tracked object per test. Every command is valid for the slots it names.
CommandWhat the driver runsPrints
make sUniquePtr<Tracked> s;nothing
make s idUniquePtr<Tracked> s(new Tracked(id));ctor id
move d sIf d is absent, UniquePtr<Tracked> d(std::move(s));. Otherwise d = std::move(s);, a self-move when d and s are the same slot.events
reset s or reset s ids.reset(); or s.reset(new Tracked(id));, constructing the Tracked firstevents
release sTracked* p = s.release();. The driver keeps p and deletes it at the end.release s id or release s null
get ss.get()get s id or get s null
bool sstatic_cast<bool>(s)bool s true or bool s false
deref s*s and s->, which must both reach the object get() returnsderef s id
drop ss goes out of scope: its destructor runs and the slot becomes absent.events
watch sFrom now on, every dtor line also shows what s holds at that moment.nothing
allocsCounts the heap allocations made inside UniquePtr members so far.allocs n
  • A later watch replaces the current one, and dropping s turns it off.
  • allocs does not count the driver's own new Tracked.

Events

A Tracked that isPrints
constructedctor id
destroyeddtor id, or while a slot is watched, dtor id (s holds k) or dtor id (s empty)
copied or movedcopy id or move id, which a correct UniquePtr never causes, since it moves pointers, not objects

The driver never returns memory to the system, so freeing an object twice shows up as a second dtor line for the same id instead of a crash.

End of input

  1. The driver prints end and turns off any watch.
  2. The remaining slots go out of scope in reverse order of creation, the way local variables die at a closing brace. A slot is created by make, by makewith (Part 2), or by move into an absent slot.
  3. Last, the driver deletes the pointers it took with release, oldest first, using plain delete.

The tests observe the order inside reset through watch. They do not observe whether move assignment destroys the old object before or after it stores the new pointer, so either order passes.

Part 1: Sole ownership

Implement the members in the table above. A correct class allocates nothing, so every allocs line prints allocs 0.

Tests 1–7 cover this part and use only UniquePtr<Tracked> with DefaultDelete.

Part 2: A custom deleter

A deleter is any object with an operator()(T*) that frees the object. The UniquePtr stores one next to the pointer and calls it wherever Part 1 destroyed an object.

  • The driver's LogDeleter carries a name. Calling it prints deleter name frees id and then deletes the object, which prints dtor id.
  • Copying or moving a LogDeleter copies its name.
MemberContractCalled by
UniquePtr(T* p, Deleter d)Owns p (which may be nullptr) and stores d.makewith
Deleter& get_deleter()The stored deleter.getdeleter s

The deleter belongs to the pointer it frees:

  1. The move constructor moves other's deleter along with the pointer.
  2. Move assignment frees *this's old object with *this's old deleter, then takes both the pointer and the deleter from other.
  3. release does not call the deleter.
CommandWhat the driver runsPrints
makewith s nameUniquePtr<Tracked, LogDeleter> s(nullptr, LogDeleter(name));nothing
makewith s name idUniquePtr<Tracked, LogDeleter> s(new Tracked(id), LogDeleter(name));ctor id
getdeleter ss.get_deleter()getdeleter s name

Every Part 1 command also works on these slots. A move never mixes the two kinds.

Tests 8–10 cover this part.

Example
Input
6
make a 1
make b 2
move b a
get a
reset b 3
release b
Output
ctor 1
ctor 2
dtor 2
get a null
ctor 3
dtor 1
release b 3
end
dtor 3
Explanation

move b a is a move assignment because slot b already exists. b takes object 1, a is left empty, and b's old object 2 is destroyed during the assignment.

reset b 3 constructs object 3, stores it in b, and then destroys object 1. release b hands object 3 back without destroying it.

At the end both slots are empty, so their destructors free nothing, and the driver deletes the released object 3 itself.