A range of memory that the operating system allocates as needed for a program. Multiple ranges can be allocated with multiple requests.
The malloc
and free
functions in C deal with the heap.
Properties, Pros and Cons
Properties | Implications |
---|---|
Size is dynamic | β
You can store a large amount of memory without worrying about stack overflows β You can store data structures of dynamic sizes π« System Call for additional memory is slow |
Inherently fragmented | β
Allows for data structure of dynamic sizes π« Requires keeping track of a vtable, which takes up a bit of cycles and memory. π« Allocations within the heap requires searching through sub-regions in the heap |
Memory has to be manually managed | π« Leading to runtime errors common to many low-level languages |
Fast accessing times if used correctly |
Note
Using a heap isnβt inherently slow. It depends on the data structure used, and the allocation strategy.
Also, an allocation may not necessarily trigger a syscall. It may not if there ranges in the heap that can fit your data structure needs.