Skip to main content
C Bug Hunt Medium +20 XP

Memory Leak

This code allocates memory but has a bug.

CFind the bug
1int *arr = malloc(5 * sizeof(int));
2for (int i = 0; i < 5; i++) arr[i] = i;
3// Program ends here
The bug is on line 3

Select the correct fix

Show answer and explanation

Answer: Add free(arr) before program ends

malloc allocates heap memory. Without free(), it leaks until the OS reclaims it. Always free what you malloc.

Hint: What must you always do with malloc'd memory?

What this challenge tests

Bug Hunt in C: memory-management, malloc, free.

Related challenges