pointers-memory · intermediate · ~15 min

Detect a use-after-free

Recognise the use-after-free pattern as a state condition.

Challenge

An operation trace uses 'a' (allocate), 'f' (free), 'u' (use) on a single resource. Implement int has_use_after_free(const char *ops) returning 1 if any 'u' happens while the resource is freed (and not re-allocated), else 0.

Starter code

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

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