Static Storage vs Heap vs Stack

The following is the summary of compiler storage allocation.

1. Static vs Dynamic

Static: Storage can be made by compiler looking only at the text of the program. One reason for statically allocating as many data objects as possible is that the addresses of these objects can be compiled into target code.

Dynamic: Storage can be made by looking at what the program does when the program is running.

2. Static

Global constants and other data generated by the compiler(e.g. info to support garbage collection) are allocated static storage. Static variables are bound to memory cells before execution begins and remains bound to the same memory cell throughout execution. E.g., C static variables.

Advantage: efficiency(direct addressing), history-sensitive subprogram support

Disadvantage: lack of flexibility, no recursion if this is the *only* kind of variable, as was the case in Fortran

3. Heap

Data that may outlive the call to the procedure that created it is usually allocated on a heap. E.g. new to create objects that may be passed from procedure to procedure.
The size of heap can not be determined at compile time. Referenced only through pointers or references, e.g., dynamic objects in C++, all objects in Java

Advantage: provides for dynamic storage management

Disadvantage: inefficient and unreliable

4. Stack

Names local to a procedure are allocated space on a stack. The size of stack can not be determined at compile time.

allows recursion
conserves storage

Overhead of allocation and deallocation
Subprograms cannot be history sensitive
Inefficient references (indirect addressing)

Leave a Comment