Bird
Raised Fist0
Cprogramming~20 mins

Memory leak concepts - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is a memory leak in C programming?
easy
A. When allocated memory is not freed after use
B. When a program runs out of variables
C. When a program crashes due to syntax errors
D. When memory is allocated too quickly

Solution

  1. Step 1: Understand memory allocation

    Memory is allocated dynamically using functions like malloc().
  2. Step 2: Identify what causes leaks

    If allocated memory is not freed using free(), it causes a memory leak.
  3. Final Answer:

    When allocated memory is not freed after use -> Option A
  4. Quick Check:

    Memory leak = not freeing allocated memory [OK]
Hint: Memory leak means forgetting to free allocated memory [OK]
Common Mistakes:
  • Confusing memory leak with syntax errors
  • Thinking memory leaks happen only on crashes
  • Believing memory leaks are about variable count
2. Which of the following is the correct way to free dynamically allocated memory in C?
easy
A. free(ptr);
B. release(ptr);
C. delete(ptr);
D. dispose(ptr);

Solution

  1. Step 1: Recall C memory management functions

    C uses malloc() to allocate and free() to release memory.
  2. Step 2: Identify correct syntax

    free(ptr); is the standard function to free allocated memory.
  3. Final Answer:

    free(ptr); -> Option A
  4. Quick Check:

    Use free() to release memory [OK]
Hint: Use free() to release memory allocated by malloc() [OK]
Common Mistakes:
  • Using delete which is C++ syntax
  • Using non-existent functions like release or dispose
  • Forgetting to pass the pointer to free()
3. What will be the output of the following C code snippet?
int *ptr = malloc(sizeof(int));
*ptr = 10;
printf("%d\n", *ptr);
// Missing free(ptr);
medium
A. Runtime crash
B. 10
C. Compilation error
D. 0

Solution

  1. Step 1: Analyze memory allocation and assignment

    Memory is allocated and 10 is stored at that location.
  2. Step 2: Check output and memory freeing

    printf prints 10 correctly; missing free() causes leak but no output error.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    Output prints stored value despite missing free() [OK]
Hint: Missing free() leaks memory but doesn't affect immediate output [OK]
Common Mistakes:
  • Assuming missing free causes crash or wrong output
  • Confusing memory leak with runtime errors
  • Expecting zero or garbage output
4. Identify the problem in this C code snippet:
char *str = malloc(20);
strcpy(str, "Hello World");
// No free(str) called
str = malloc(30);
strcpy(str, "New String");
free(str);
medium
A. No problem, code is correct
B. Syntax error in strcpy usage
C. Double free error
D. Memory leak due to first malloc not freed

Solution

  1. Step 1: Trace memory allocations

    First malloc(20) is assigned to str, then overwritten by second malloc(30).
  2. Step 2: Check freeing of memory

    First allocated memory is lost without free, causing memory leak.
  3. Final Answer:

    Memory leak due to first malloc not freed -> Option D
  4. Quick Check:

    Overwriting pointer without free causes leak [OK]
Hint: Always free old memory before reassigning pointer [OK]
Common Mistakes:
  • Thinking strcpy causes syntax error
  • Assuming double free without repeated free calls
  • Believing code is leak-free because free is called once
5. You have a function that allocates memory for an integer array and returns it. Which practice helps prevent memory leaks when using this function multiple times?
hard
A. Use global variables to store allocated memory
B. Never call free() because the function handles it internally
C. Always call free() on the returned pointer before reassigning it
D. Allocate memory only once and never reuse pointers

Solution

  1. Step 1: Understand ownership of allocated memory

    The caller owns the memory returned and must free it to avoid leaks.
  2. Step 2: Prevent leaks by freeing before reassignment

    Calling free() before reassigning pointer prevents losing track of allocated memory.
  3. Final Answer:

    Always call free() on the returned pointer before reassigning it -> Option C
  4. Quick Check:

    Free before reuse prevents leaks [OK]
Hint: Free returned memory before pointer reuse [OK]
Common Mistakes:
  • Assuming function frees memory automatically
  • Using global variables without freeing
  • Avoiding reuse but causing inefficient memory use
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard