21E. Memory Leaks

Some languages, such as Java, use garbage collectors that periodically go through the heap and recycle chunks of memory that are not being used. That does not happen in a C++ program. If you lose all pointers to a chunk of memory without deallocating that memory then you have a memory leak. Your program will continue to own that memory, but has no way of ever using it again. A very small memory leak is not a problem. But if a program allocates a lot of memory and never deallocates it, the program's memory footprint will grow and grow.

When your program stops, all of the memory that it owns is automatically recovered by the operating system. So a memory leak is only relevant while a program is running.

The standards for this course require that your program have no memory leaks. You will not lose points for minor memory leaks such as memory allocated once in main and not deleted.

Exercises

  1. What is a memory leak? Answer

  2. What are the consequences of a memory leak? Answer