A memory leak happens when a program allocates memory but never frees it, causing the memory to stay used and unavailable for other parts of the program or system.
Click to reveal answer
beginner
Why are memory leaks a problem in programs?
Memory leaks reduce the available memory over time, which can slow down or crash programs because they run out of memory to use.
Click to reveal answer
beginner
Which C functions are commonly used to allocate and free memory?
malloc() is used to allocate memory, and free() is used to release that memory back to the system.
Click to reveal answer
intermediate
What happens if you call free() on a pointer twice?
Calling free() twice on the same pointer can cause undefined behavior, which may crash the program or corrupt memory.
Click to reveal answer
intermediate
How can you detect memory leaks in C programs?
You can use tools like Valgrind or AddressSanitizer to find memory leaks by tracking allocated memory that was never freed.
Click to reveal answer
What does a memory leak cause in a program?
AProgram runs faster
BMemory stays allocated and is never freed
CMemory is freed too early
DMemory is shared between programs
✗ Incorrect
A memory leak means memory is allocated but not freed, causing it to stay used.
Which function is used to free allocated memory in C?
Afree()
Bdelete()
Calloc()
Dmalloc()
✗ Incorrect
free() releases memory that was allocated with malloc() or similar functions.
What is a common cause of memory leaks?
ACalling free() too many times
BUsing global variables
CForgetting to call free() after malloc()
DWriting to a file
✗ Incorrect
Not freeing allocated memory causes memory leaks.
Which tool can help detect memory leaks in C programs?
AValgrind
BGCC
CMake
DGit
✗ Incorrect
Valgrind tracks memory usage and can find leaks.
What happens if you free memory twice?
AMemory is automatically reallocated
BProgram runs normally
CMemory is doubled
DUndefined behavior, possible crash
✗ Incorrect
Freeing memory twice can cause crashes or corrupt memory.
Explain what a memory leak is and why it is harmful in C programs.
Think about what happens when memory is allocated but never given back.
You got /3 concepts.
Describe how you can prevent memory leaks when using malloc() in C.
Consider the steps after allocating memory.
You got /4 concepts.
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
Step 1: Understand memory allocation
Memory is allocated dynamically using functions like malloc().
Step 2: Identify what causes leaks
If allocated memory is not freed using free(), it causes a memory leak.
Final Answer:
When allocated memory is not freed after use -> Option A
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
Step 1: Recall C memory management functions
C uses malloc() to allocate and free() to release memory.
Step 2: Identify correct syntax
free(ptr); is the standard function to free allocated memory.
Final Answer:
free(ptr); -> Option A
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?
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
Step 1: Trace memory allocations
First malloc(20) is assigned to str, then overwritten by second malloc(30).
Step 2: Check freeing of memory
First allocated memory is lost without free, causing memory leak.
Final Answer:
Memory leak due to first malloc not freed -> Option D
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
Step 1: Understand ownership of allocated memory
The caller owns the memory returned and must free it to avoid leaks.
Step 2: Prevent leaks by freeing before reassignment
Calling free() before reassigning pointer prevents losing track of allocated memory.
Final Answer:
Always call free() on the returned pointer before reassigning it -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.