Skip to main content

Practice · 1 of 4

Create/Destroy with a Liveness Counter

CHALLENGE
Difficulty: advanced+25 XP

A file-scope int gadgets_alive = 0; is already in your editor. Implement gadget_t *gadget_create(int id) (heap-allocate, set id, increment the counter), int gadget_id(const gadget_t *g), and void gadget_destroy(gadget_t *g) (decrement, free; NULL is a no-op). The counter is the contract, made executable.

Back to lesson: Practice: Ownership Contract Drills