pointers-memory · intermediate · ~15 min

Are allocations balanced?

Model alloc/free balance — the essence of leak-freedom.

Challenge

Given an operation trace where 'a' = allocate and 'f' = free, implement int balanced_allocs(const char *ops) returning 1 if every allocation is eventually freed and no free happens without a live allocation (depth never goes negative, ends at 0).

Starter code

int balanced_allocs(const char *ops) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.