C Bug Hunt Hard +35 XP
Double Free
This code causes heap corruption.
CFind the bug
1
int *p = malloc(sizeof(int));2
*p = 42;3
free(p);4
free(p); The bug is on line 4
Select the correct fix
Show answer and explanation
Answer: Both A and B — set NULL to prevent double free
Freeing already-freed memory is undefined behavior. After free(p), set p = NULL so a second free(NULL) is a no-op.
Hint: free(NULL) is safe. free(freed_pointer) is not.
What this challenge tests
Bug Hunt in C: memory, double-free, heap.