Skip to main content
C Bug Hunt Hard +35 XP

Double Free

This code causes heap corruption.

CFind the bug
1int *p = malloc(sizeof(int));
2*p = 42;
3free(p);
4free(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.

Related challenges