Avoiding Memory Leaks

You already read that it is an error if a block of memory is allocated but never returned to the pool. Such a block will be freed only when the program terminates. If the program runs for a long time and continuously allocates new blocks while forgetting to give old unused blocks back to the pool, then the program will run out of memory at some point, causing subsequent malloc() requests to fail.

If a program has such a bug, it is said to have a memory leak, because the pool of available memory has a leak in it (Figure 10.13).

Figure 10.13. Memory leaks occur when reserved blocks are not freed by an application, thereby limiting the amount of available memory.

The address returned by malloc() is the ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.