Static Memory Allocation

Static Memory Allocation and Dynamic Memory Allocation

Introduction to Static Memory Allocation

Firstly, Static Memory Allocation is performed during the compilation stage. Moreover, in this allocation we declare variables statically. Compiler decides the extent of storage which cannot be change with time. Certainly, memory allocation is important for storing values when we assign them to variables.

There are two types of memory allocation in a program:

  • Stack Memory
  • Heap Memory

Firstly, stack memory is compile time execution. This is static allocation.

Secondly, heap memory is execution at run time. This is dynamic allocation.

Key features of Static Memory Allocation

  • Firstly, variable allocation is fix or permanent.
  • Secondly, in this process we allocate memory before program execution.
  • Thirdly, uses stack data structure
  • Moreover, there might be wastage of memory.
  • No memory reusability.
Static Memory Allocation

For instance, we take array of size 10 and store values for 8 variables. Hence, there is wastage of memory for two blocks.

  • int a[10];
  • for ( i=1; i<=8; i++)
  • {
  • scanf(“%d”, &a[i]);
  • }

Thus, one main disadvantage of using static memory allocation is that there can be wastage of memory.

Dynamic Memory Allocation

Firstly, in dynamic memory allocation, memory allocation is done at the time of execution i.e. at run time. Secondly, Functions like calloc() and malloc() supports dynamic allocation.

malloc() takes one argument i.e. size of bytes wheras calloc() takes two arguments i.e. size of bytes and total number of blocks. Other function is realloc() to reallocate the memory.

Moreover, one main advantage of DMA is that there is no memory wastage.

Key features of Dynamic Memory Allocation

  • Firstly, there is no wastage of memory.
  • Secondly, there is no upper limit of memory space.
  • Thirdly, in this process we allocate memory at run time.
  • Uses heap data structure
  • Insertion and deletion is done easily just by manipulation of addresses.
  • Uses linked lists.

Static Memory Allocation vs Dynamic Memory Allocation

Static Memory AllocationDynamic Memory Allocation
Variable allocation is fix or permanent.Variable allocation is dynamic.
Allocation of memory before program execution.Allocation of memory during program execution
Uses stack for memory.Uses heap for memory.
Less efficient because of memory wastage.Relatively more efficient.
Example: arraysExample: linked lists

Conclusion

In conclusion, we have learnt that Static Memory Allocation is performed during the compilation stage. Moreover, in this allocation we declare variables statically. In this process we allocate memory before program execution. It uses stack data structure.

In dynamic memory allocation, memory allocation is done at the time of execution i.e. at run time. Secondly, Functions like calloc() and malloc() supports dynamic allocation. In this process variable allocation is dynamic. It uses heap data structure.

Lastly, we have seen the major differences between both these allocation techniques.

About the author

Drishti Patel

View all posts
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments