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?
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
intermediate
How do you prevent memory leaks in C++?
You prevent memory leaks by always freeing memory you allocate with 'delete' or 'delete[]' and by using smart pointers that automatically manage memory.
Click to reveal answer
beginner
What is an example of a memory leak in C++?
If you use 'new' to create an object but never use 'delete' to free it, the memory stays allocated and causes a leak.
Click to reveal answer
intermediate
What tools can help detect memory leaks in C++?
Tools like Valgrind or AddressSanitizer can find memory leaks by tracking allocated memory and checking if it is properly freed.
Click to reveal answer
What causes a memory leak in C++?
AWriting to a file
BUsing variables inside functions
CAllocating memory with 'new' and not freeing it with 'delete'
DDeclaring too many variables
✗ Incorrect
Memory leaks happen when allocated memory is not freed, like using 'new' without 'delete'.
Which C++ feature helps automatically manage memory and avoid leaks?
AGlobal variables
BSmart pointers
CMacros
DInline functions
✗ Incorrect
Smart pointers automatically free memory when no longer needed, preventing leaks.
What happens if a program has a memory leak?
AIt runs faster
BIt reduces CPU usage
CIt frees memory automatically
DIt uses more memory over time and may crash
✗ Incorrect
Memory leaks cause programs to use more memory and can lead to crashes.
Which tool can help find memory leaks in C++ programs?
AValgrind
BGCC compiler
CGit
DMakefile
✗ Incorrect
Valgrind detects memory leaks by monitoring memory allocation and freeing.
What must you do after using 'new' in C++ to avoid leaks?
AUse 'delete' to free the memory
BUse 'malloc' instead
CDo nothing, memory frees automatically
DUse 'cout' to print the memory
✗ Incorrect
You must use 'delete' to free memory allocated with 'new' to avoid leaks.
Explain what a memory leak is and why it is important to avoid it in C++ programs.
Think about what happens when memory is allocated but never freed.
You got /3 concepts.
Describe how smart pointers help prevent memory leaks in C++.
Smart pointers act like helpers that clean up memory for you.
You got /3 concepts.
Practice
(1/5)
1.
What is a memory leak in C++?
easy
A. When variables are declared but not used
B. When a program runs too fast
C. When allocated memory is not released back to the system
D. When the program uses too many functions
Solution
Step 1: Understand memory allocation
Memory allocated with new must be freed with delete.
Step 2: Define memory leak
If memory is allocated but not freed, it stays reserved and causes a leak.
Final Answer:
When allocated memory is not released back to the system -> Option C
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 slow program
Thinking unused variables cause leaks
Mixing memory leak with logical errors
2.
Which of the following C++ code snippets correctly frees allocated memory?
int* ptr = new int(5);
// What should be done next?
easy
A. free(ptr);
B. delete ptr;
C. ptr = nullptr;
D. ptr = new int(10);
Solution
Step 1: Identify allocation method
Memory is allocated with new, so it must be freed with delete.
Step 2: Match correct freeing syntax
free() is for malloc(), not new. Setting pointer to nullptr does not free memory.
Final Answer:
delete ptr; -> Option B
Quick Check:
Use delete for new, free for malloc [OK]
Hint: Use delete for new, not free [OK]
Common Mistakes:
Using free() with new
Just setting pointer to nullptr without delete
Allocating new memory without deleting old
3.
What is the output of this C++ code?
int* ptr = new int(10);
ptr = new int(20);
std::cout << *ptr << std::endl;
medium
A. 10
B. Compilation error
C. Undefined behavior
D. 20
Solution
Step 1: Analyze pointer reassignment
First, ptr points to 10. Then ptr is reassigned to new int(20) without deleting old memory.
Step 2: Determine output
Pointer now points to 20, so output is 20. The old memory is leaked but does not affect output.
Final Answer:
20 -> Option D
Quick Check:
Pointer points to last new value = 20 [OK]
Hint: Pointer shows last assigned value, old memory leaks [OK]
Common Mistakes:
Expecting output 10 after reassignment
Thinking code causes compile error
Confusing memory leak with runtime error
4.
Find the memory leak in this code and fix it:
void func() {
int* arr = new int[5];
arr = new int[10];
delete[] arr;
}
medium
A. Delete first array before reassigning arr
B. Use delete instead of delete[]
C. No leak, code is correct
D. Change new int[10] to new int(10)
Solution
Step 1: Identify memory allocations
First, arr points to array of 5 ints, then reassigned to array of 10 ints without deleting first.
Step 2: Fix memory leak
Delete the first array before reassigning arr to avoid leak. Use delete[] for arrays.
Final Answer:
Delete first array before reassigning arr -> Option A
Quick Check:
Delete old array before new assignment [OK]
Hint: Delete old array before assigning new to pointer [OK]
Common Mistakes:
Not deleting first allocated array
Using delete instead of delete[] for arrays
Ignoring memory leak warnings
5.
You have a class that allocates memory in its constructor using new. Which approach best prevents memory leaks?
hard
A. Use smart pointers like std::unique_ptr to manage memory automatically
B. Manually call delete in destructor and follow Rule of Three
C. Use malloc and free instead of new and delete
D. Never use new, only stack variables
Solution
Step 1: Understand manual memory management risks
Manually calling delete and following Rule of Three is error-prone and can cause leaks if forgotten.
Step 2: Use smart pointers for safety
Smart pointers automatically free memory when no longer used, preventing leaks safely and easily.
Final Answer:
Use smart pointers like std::unique_ptr to manage memory automatically -> Option A
Quick Check:
Smart pointers prevent leaks automatically [OK]
Hint: Use smart pointers to avoid manual delete [OK]
Common Mistakes:
Relying only on manual delete
Avoiding new but not managing memory
Mixing malloc/free with new/delete
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.