0
0
Cprogramming~20 mins

Memory leak concepts - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memory Leak Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of code with missing free() call
What is the output of this C program snippet?
C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *ptr = malloc(sizeof(int));
    *ptr = 10;
    printf("%d\n", *ptr);
    // missing free(ptr);
    return 0;
}
ACompilation error
B0
C10
DRuntime error
Attempts:
2 left
💡 Hint
Think about what happens if you allocate memory but don't free it.
🧠 Conceptual
intermediate
2:00remaining
Cause of memory leak in this code
Which line in the following code causes a memory leak?
C
#include <stdlib.h>

void func() {
    int *p = malloc(4 * sizeof(int));
    p = malloc(10 * sizeof(int));
    free(p);
}
Aint *p = malloc(4 * sizeof(int));
BNo memory leak
Cfree(p);
Dp = malloc(10 * sizeof(int));
Attempts:
2 left
💡 Hint
Think about what happens to the first allocated memory.
🔧 Debug
advanced
2:00remaining
Identify the memory leak in this function
What is the memory leak issue in this function?
C
#include <stdlib.h>
#include <string.h>

char* create_string() {
    char *str = malloc(20);
    if (!str) return NULL;
    str = "Hello";
    return str;
}
AMemory allocated by malloc is overwritten by string literal assignment causing leak
Bmalloc size is too small
CMissing free(str) before return
DNo memory leak, code is correct
Attempts:
2 left
💡 Hint
Look at what happens to the pointer after malloc.
📝 Syntax
advanced
2:00remaining
Which option causes a compile-time error?
Which of the following code snippets will cause a compile-time error?
Aint *p = malloc(sizeof(int) * 5); p = NULL; free(p);
Bint *p; free(p);
Cint *p = malloc(sizeof(int) * 5); free(p);
Dint *p = malloc(sizeof(int) * 5); free(p); free(p);
Attempts:
2 left
💡 Hint
Consider if the pointer is initialized before free.
🚀 Application
expert
3:00remaining
How many memory leaks occur in this program?
How many memory leaks occur when this program runs?
C
#include <stdlib.h>

int main() {
    int *a = malloc(10 * sizeof(int));
    int *b = malloc(5 * sizeof(int));
    a = b;
    free(b);
    return 0;
}
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Check which allocated memory is lost without free.