In any complex system—whether engineered or natural—products are constructed, distributed, used, and finally recycled. The lifetimes of such products are typically independent of the manufacturing process. Data structures are the primary (intermediate) products of complex programs, and data structures require memory. However, the memory that we have used so far—the stack—is not well suited for creating data structures whose existence is independent of functions’ execution periods. Stack frames form the stack, and the lifetimes of stack frames, by definition, correspond to function execution periods: a frame is pushed on the stack at the beginning of a function’s execution and popped from the stack at the end of the function’s execution; any data structure that resides in the stack frame is then lost.
This chapter introduces a sector of program memory, called the heap, specifically intended for producing data structures whose lifetimes are independent of the execution periods of the functions that create and manipulate them. Dynamic memory allocation is the process of obtaining segments of memory from the heap for use.
KeywordsApplication Programming Interface Unit Test Memory Leak Execution Period Dynamic Memory Allocation
Unable to display preview. Download preview PDF.