Recall & Review
beginner
What is a memory leak in C++?
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++?
✗ 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?
✗ Incorrect
Smart pointers automatically free memory when no longer needed, preventing leaks.
What happens if a program has a memory leak?
✗ Incorrect
Memory leaks cause programs to use more memory and can lead to crashes.
Which tool can help find memory leaks in C++ programs?
✗ Incorrect
Valgrind detects memory leaks by monitoring memory allocation and freeing.
What must you do after using 'new' in C++ to avoid leaks?
✗ 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.