140 likes | 633 Views
Run-Time Storage Organization. Introduction. We need to relate the static source text of a program to the actions that must occur at run time We need to allocate and manage the memory used by the program (static and dynamic). Memory Allocation Techniques. Static Allocation Stack Allocation
E N D
Introduction • We need to relate the static source text of a program to the actions that must occur at run time • We need to allocate and manage the memory used by the program (static and dynamic)
Memory Allocation Techniques • Static Allocation • Stack Allocation • Heap Allocation Code Static Data Stack Heap
Static Allocation • The number and size of all objects to be allocated is known at compile time • It is used for: • Global variables that are fixed in size and accessible throughout program execution • Program literals (constants) • Program code
Stack Allocation • Information needed by a single execution of a routine (a procedure, function or method) is managed using an activation record or frame • An activation record contains local variables, parameters, return address, etc. • Activation records are pushed and popped as functions calls begin and end
Parameter Passing • Call-by-value can be implemented as follows: • A formal parameter is treated like a local variable, so the storage for the formal is in the activation record of the called routine • The caller evaluates the actual parameters and places their values in the storage for the formals • Call-by-reference • The caller passes to the called procedure a reference to the storage address of each actual parameter
Heap Allocation • The heap is used to store data which can be allocated and freed at any time and in any order • Deallocation is either manual or automatic (garbage collection) • The heap typically becomes fragmented during use