Heap

From Rosetta Code
Revision as of 23:05, 12 October 2008 by rosettacode>Mwn3d (Computer engineers are good for something)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The heap is a pool of memory used by computer programs. Many of the variables declared in programs (like those malloc'd in C or new'd in Java or C++) are allocated on the heap. The data for these variables is accessed indirectly, usually through references. Allocation on the heap is seemingly disorganized, which may be where it gets its name (a "heap" is basically just a big pile).

Memory allocated on the heap must be deallocated (or freed) once its purpose has been served if the programmer wants that particular memory location again. This may be done via automatic garbage collection or explicitly freeing the memory through references to it. "Memory leaks" are frequently instances where a branch in a program allocates memory on the heap without freeing it later, leaving unreferenced data in the heap with no way of recovering its memory location.

Memory not allocated on the heap is allocated on the system stack, and includes things like subroutine return values and parameters, the return address of a subroutine, and local temporary variables, among other things.