There are mainly three types of Storage Allocation Strategies which compiler uses:
Static Storage Allocation
*storage -allocation decision is static, if it can be made by the compiler looking only at the text of the
program
*Static storage allocation is the simplest form of memory allocation. In this strategy, the memory for
variables is allocated at compile time.
*The compiler determines the memory addresses for all variables before the program starts running,
and these addresses remain the same throughout the program’s execution.
*For example, in languages like C or C++, when you declare global variables or static variables, the
memory for these variables is allocated using static storage.
int number = 1; // Global
static int digit = 1;
In static allocation, names are bound to storage as the program is compiled, so there is no need for a
run-time support package.
• Since the bindings do not change at run-time, everytime a procedure is activated, its names
are bound to the same storage locations.
• Therefore values of local names are retained across activations of a procedure.
• That is, when control returns to a procedure the values of the locals are the same as they were
when control left the last time.
• From the type of a name , the compiler decides the amount of storage for the name
Advantages of Static Storage Allocation
The allocation process is straightforward and easy to understand.
Disadvantages of Static Storage Allocation
The size of the data must be known at the compile time.
storage-allocation decision is dynamic if it can be decided only while the program is running.
Dynamic Storage Allocation Techniques:
1. Stack storage
2. Heap storage
Stack Storage Allocation
Stack storage allocation is used for memory allocation of local variables of a function (including main),
where memory is allocated at when the function is called.
In this case, memory is assigned in a Last In, First Out (LIFO) manner, meaning that memory is
allocated when a function is called and deallocated when the function returns.
Almost all compilers for languages that use procedures, functions, or methods manages at least part
of their run-time memory as a stack.
• Each time a procedure is called, space for its local variables is pushed onto a stack, and when the
procedure terminates, that space is popped off the stack
Stack Storage Allocation - Activation
Trees
• The activations of procedures during the running of an entire program is represented by a tree,
called an activation tree.
• Each node corresponds to one activation, and the root is the activation of the "main" procedure
that initiates execution of the program
Stack Storage Allocation- Control Stack
procedure calls and run time stack are managed by a runtime stack called control stack
Activation record
An activation record is used to store information about the status of the machine, such as the value
of the program counter and machine registers, when a procedure call occurs
Stack Storage Allocation- Contents of an Activation Record
1. Temporary values - such as those arising from the evaluation of expressions
2. Local data belonging to the procedure whose activationrecord this is.
3. A saved machine status, with information about the state of the machine just before the call to the
procedure.
4. An "access link" may be needed to locate data needed by the called procedure but found
elsewhere, e.g., in another activation record.
5. A control link, pointing to the activation record of the caller.
6. Space for the return value of the called function, if any.
7. The actual parameters used by the calling procedure.
Advantages of Stack Storage Allocation
Stack allocation provides very fast memory access and management.
Disadvantages of Stack Storage Allocation
Stack has a limited size which can lead to potential stack overflow errors
Heap Storage Allocation
Heap storage allocation is another form of dynamic memory allocation, but unlike the stack, it allows
memory to be allocated at runtime for variables that can grow or shrink during execution.
The heap is used for memory that is not automatically deallocated when a function call ends. This type
of memory allocation is used when you need to store objects or data that are not tied to a specific
function scope.
In C/C++, functions like malloc() or new are used to allocate memory in the heap, and functions like
free() or delete are used to deallocate the memory.
int ans = new int[5];*
The heap is the portion of the store that is used for data that lives indefinitely, or until the program
explicitly deletes it
Java uses new operator to create objects that may be passed from procedure to procedure, so they
continue to exist long after the procedure that created them is gone. Such objects are stored on a
heap
When a program requests memory for a variable or object, the memory manager produces a chunk
of contiguous heap memory of the requested size.
• If possible, it satisfies an allocation request using free space in the heap;
• if no chunk of the needed size is available, it seeks to increase the heap storage space by
getting consecutive bytes of virtual memory from the operating system.
• If space is exhausted, the memory manager passes that information back to the application
program
Advantages of Heap Storage Allocation
Heap allocation is useful when we have data whose size is not fixed and can change during the run
time
Disadvantages of Heap Storage Allocation
Heap allocation is slower as compared to stack allocation.