pointers-memory · intermediate · ~15 min

Validate a resource lifecycle

Encode all the memory-safety rules as a small state machine.

Challenge

With the same alphabet ('a','f','u'), implement int valid_lifecycle(const char *ops) returning 1 only if the sequence is valid: never use/free before allocating, never double-free, never use-after-free. 'a' is only valid when not currently allocated-and-live.

Starter code

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

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