Skip to main content

Practice · 3 of 4

The Cleanup Ladder

CHALLENGE
Difficulty: advanced+25 XP

File-scope counters int a_alive = 0, b_alive = 0, c_alive = 0; are already in your editor. Implement int cleanup_ladder(int fail_at) simulating a three-resource acquisition: allocate A (a_alive++), then B (b_alive++), then C (c_alive++); if fail_at is 1, 2, or 3, simulate failure *before* that resource's allocation, releasing everything acquired so far in reverse order and returning -1; success returns 0 with everything released too. Every path must leave all counters at 0.

Back to lesson: Practice: Ownership Contract Drills