0
0
Cprogramming~5 mins

Memory leak concepts - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a memory leak in C programming?
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
Which function is used to free allocated memory in C?
Afree()
Bdelete()
Calloc()
Dmalloc()
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
Which tool can help detect memory leaks in C programs?
AValgrind
BGCC
CMake
DGit
What happens if you free memory twice?
AMemory is automatically reallocated
BProgram runs normally
CMemory is doubled
DUndefined behavior, possible crash
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.