What is Memory leak?

A defect in which memory obtained with malloc or similar is not released with free after use, and the pointer to it is lost, so the usable memory keeps shrinking. It often happens when an error path returns early and skips the release. In a short program the OS reclaims the memory at exit, so it goes unnoticed, but in servers and embedded devices that run for a long time free memory decreases little by little until an allocation finally fails. Tools such as Valgrind and AddressSanitizer (LeakSanitizer) can detect it.

Japanese name: メモリリーク

Memory leak appears in 2 practice questions across 2 topics, including Linkers, Loaders & Executables, C Programming Basics.

Field: C Programming Basics

Try it: questions about Memory leak

Memory allocated with C's malloc generally comes from which area of a process?

  1. AThe .text section
  2. BThe heap
  3. CThe .rodata section
Show answer and explanation

Answer: B. The heap

The heap is the area from which a running program obtains memory of the size it needs and returns it with free when done. Its key feature is that sizes and lifetimes are decided at run time. malloc manages free space itself and asks the OS to enlarge the area when it runs short (through the brk or mmap system calls on Linux). Forgetting to call free leaves unused memory behind, which is a memory leak.

Memory leak in the explanations

Questions that mention Memory leak (2)

  1. Memory allocated with C's malloc generally comes from which area of a process?
  2. fill and use are functions defined elsewhere. Which of the following is a problem with the function above?

Books on C Programming Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Dynamic memory allocation Heap memory Alignment and padding Bit-field Buffer overflow Dangling pointer Function pointer Integer promotion Macro (#define) Pointer

Back to the glossary