0% found this document useful (0 votes)
41 views4 pages

Static Allocation

Static allocation assigns memory to data objects at compile time based on their size, it does not allow dynamic allocation, and names of data objects are fixed, whereas heap allocation uses a linked list to manage dynamic allocation and deallocation of objects and data structures at runtime using best-fit memory management. Stack allocation assigns memory to function variables from a contiguous block in a last-in, first-out manner, with allocation and deallocation occurring automatically during function calls and returns.

Uploaded by

Ali Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views4 pages

Static Allocation

Static allocation assigns memory to data objects at compile time based on their size, it does not allow dynamic allocation, and names of data objects are fixed, whereas heap allocation uses a linked list to manage dynamic allocation and deallocation of objects and data structures at runtime using best-fit memory management. Stack allocation assigns memory to function variables from a contiguous block in a last-in, first-out manner, with allocation and deallocation occurring automatically during function calls and returns.

Uploaded by

Ali Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

• The period of time from creation to destruction is called the

LIFETIME of a binding

– If object outlives binding it's garbage

– If binding outlives object it's a dangling reference

• The textual region of the program in which the binding is active is


its scope

• Storage Allocation mechanisms

– Static

– Stack

Heap

Static allocation
Static allocation is an allocation procedure which is used
for allocation of all the data objects at compile time. In this
type of allocation, allocation of data objects is done at
compile time only. As in static allocation compiler decides
the extent of storage which cannot be change with time,
hence it is easy for the compiler to know the addresses of
these data objects in the activation records at later stage.
Static allocation is implemented in FORTRAN.
Heap Allocation :
Heap allocation is an allocation procedure in which heap
is used to manage the allocation of memory. Heap helps
in managing the dynamic memory allocation. In heap
allocation, creation of dynamic data objects and data
structures is also possible as same as stack allocation.
Heap allocation overcomes the limitation of stack
allocation. It is possible to retain the value of variables
even after the activation record in heap allocation strategy
which is not possible in stack allocation. It maintains a
linked list for the free blocks and reuse the deallocated
space using best fit.

The memory is allocated during execution of instructions


written by programmers. Note that the name heap has
nothing to do with heap data structure. It is called heap
because it is a pile of memory space available to
programmers to allocated and de-allocate. If a
programmer does not handle this memory well, memory
leak can happen in the program.
Stack Allocation :
The allocation happens on contiguous blocks of memory. We
call it stack memory allocation because the allocation happens
in function call stack. The size of memory to be allocated is
known to compiler and whenever a function is called, its variables
get memory allocated on the stack

its variables get memory allocated on the stack. And


whenever the function call is over, the memory for the
variables is deallocated. This all happens using some
predefined routines in compiler. Programmer does not
have to worry about memory allocation and deallocation
of stack variables.

Static
Allocation Heap Allocation

Heap allocation make use of


Static allocation allocates heap for managing the
memory on the basis of size allocation of memory at run
1. of data objects. time.

In static allocation, there is


no possibility of creation of In heap allocation, dynamic
dynamic data structures data structures and objects are
2. and objects. created.

In static allocation, the


names of the data objects Heap allocation allocates
are fixed with storage for contiguous block of memory to
3. addressing. data objects.

Static allocation is simple,


but not efficient memory Heap allocation does memory
4. management technique. management in efficient way.

Static allocation strategy is While heap allocation is slow in


faster in accessing data as accessing as there is chance of
compared to heap creation of holes in reusing the
5. allocation. free space.

Static allocation is
inexpensive, it is easy to While heap allocation is
6. implement. comparatively expensive.
Key Differences Between Stack and Heap Allocations
1. In a stack, the allocation and deallocation is automatically done by
whereas, in heap, it needs to be done by the programmer manually.
2. Handling of Heap frame is costlier than handling of stack frame.
3. Memory shortage problem is more likely to happen in stack whereas the
main issue in heap memory is fragmentation.
4. Stack frame access is easier than the heap frame as the stack have small
region of memory and is cache friendly, but in case of heap frames which
are dispersed throughout the memory so it cause more cache misses.
5. Stack is not flexible, the memory size allotted cannot be changed
whereas a heap is flexible, and the allotted memory can be altered.
6. Accessing time of heap takes is more than a stack.

You might also like