pointers-memory · intermediate · ~15 min
Save-next-before-free pattern — the universal linked-structure destroy idiom.
Given a linked list typedef struct node { int v; struct node *next; } node_t;,
implement void destroy(node_t *head) that frees every node in the list.
The naïve version is wrong:
while (head) { free(head); head = head->next; } // UAF on the next iteration
Write the correct version that saves next before free.
The naïve destroy loop reads n->next AFTER free(n) — a textbook use-after-free. Doing it right is the single most-asked memory-safety question.
Head pointer (may be NULL).
All nodes freed.
No allocations. Must run cleanly under ASan.
typedef struct node { int v; struct node *next; } node_t;
void destroy(node_t *head) { /* TODO */ }
Reading head->next after free(head) — use-after-free.
NULL head (no-op); single-node list.
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.