Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Static allocation
2.1.
Advantages
2.2.
Limitation
3.
Static vs Dynamic allocation
4.
Frequently Asked Questions
4.1.
What is used for dynamic allocation?
4.2.
What is the main drawback of static allocation?
5.
Conclusion
Last Updated: Mar 27, 2024

Static Allocation

Author Malay Gain
1 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM
Compiler Design

Introduction

Static memory allocation is known as Compile time memory allocation, which is used for the allocation of memory during the process of compilation of data of fixed size.

The compiler allocates memory for the variables present in the program. Static variables are permanently allocated during compilation.

Also see, Static and Dynamic Memory Allocation

Static allocation

Static allocation stands for the data allocated at a memory place with both known size as well as an address at compile time. Furthermore, the memory allocated stays allocated throughout the execution of the program. In most modern computers, their logical address space is divided into a data section (used for data) and a text section (used for code). Assemblers (i.e programs that convert symbolic machine code into binary machine code)  maintain current address pointers to both the data area and text area. They also have pseudo instructions (directives) that can place labels at these addresses and move them. So we can allocate space for an array in the data space by placing a label at the current-address pointer in the data space and then moving the current-address pointer up by the size of the array. The code can use the label to access the array.

Also See, Top Down Parsing

Advantages

  • Static memory allocation allocates memory to a variable that is declared ahead of time. 
  • Execution time is efficiently slower.
  • It is easy to use in the context of programming. 

Limitation

  • The size of an array must be known at compile time if it is statically allocated, and the size can not change during execution.
  • Though the array is only in use for a small fraction of the time, the space is allocated throughout the entire execution of the program.
  • There is little reuse of statically allocated space within one program execution. The Fortran allows the programmer to specify that several statically allocated arrays can share the same space, but in modern languages, this feature is rarely seen.

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Static vs Dynamic allocation

  • Static allocation allows allocation of memory at compile time. 
  • In Dynamic allocation, memory is allocated during run-time.
  • Static allocation uses the stack for memory management, but Dynamic allocation uses the heap for memory management.
  • In the case of static allocation, allocated memory can not be changed during the execution of the program. But in the case of dynamic allocation, allocated memory can be changed during the program's execution.
  • Static allocation is faster in nature, whereas dynamic allocation is slower.

 

Frequently Asked Questions

What is used for dynamic allocation?

Heap is used to allocating memory during dynamic allocation.

What is the main drawback of static allocation?

Memory can't be reused or resized after allocation.

Conclusion

This article covered static memory allocation and its limitation.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Cheers!

Live masterclass