Chapter 6: Runtime System

Language Facility for Dynamic Storage Allocation

. Storage is usually taken from heap

. Allocated data is retained until deallocated

. Allocation can be either explicit or implicit

- Pascal : explicit allocation and de-allocation by new() and dispose()

- Lisp : implicit allocation when cons is used, and de- allocation through garbage collection

Static storage allocation is usually done on the stack, as this is a convenient way to take care of the normal scoping rules, where the most recent values have to be considered, and when the scope ends, their values have to be removed. But for dynamic allocation, no such prior information regarding the use of the variables is available. So we need the maximum possible flexibility in this. For this a heap is used. For the sake of a more efficient utilization of memory, the stack grows downwards and the heap grows upwards, starting from different ends of the available memory. This makes sure that all available memory is utilized. Pascal allows for explicit allocation and de-allocation of memory. This can be done by using the new() and dispose() functions. However, in Lisp, continuous checking is done for free memory. When less than 20 percent of the memory is free, then garbage collection is performed. In garbage collection, cells that can no longer be accessed are de-allocated. (Storage that has been allocated but can no longer be accessed is called 'garbage'.)