Static Versus Dynamic Storage Allocation
Storage management is concerned with the layout and allocation of data to memory locations in a runtime environment. Since the same name in a program text might refer to several locations at run time, these difficulties are tricky.
Compile-time and runtime are distinguished by the terms static and dynamic, respectively. A storage-allocation decision is said to be static if it can be decided by the compiler based just on the program's text, rather than what the program does as it executes. On the other hand, a decision is dynamic if it can only be made while the program is running.
Static Storage
- Fully static runtime environments may be helpful for languages that do not support pointers, dynamic allocation, or recursive function calls.
- Names are connected to storage locations in static allocation. If memory is generated at compile-time, it will be created only once in the static area.
- The dynamic data structure is supported by static allocation, which means memory is allocated only at build time and released after the application is finished.
Also See, Top Down Parsing
For dynamic storage allocation, several compilers employ a combination of the two strategies listed below:
Stack Storage
- Storage is organized as a stack in static storage allocation.
-
When activation begins, an activation record is pushed into the stack, and when it ends, it is popped.
-
It runs on the last-in-first-out (LIFO) principle, and this allocation supports the recursion process.
Heap Storage
-
The most flexible allocation strategy is Heap Allocation.
- Memory allocation and deallocation can occur at any time and in any location, depending on the user's needs.
- Heap allocation is a technique for dynamically allocating memory to variables and claiming it back when the variables are no longer used.
- It supports the recursion process.
You can also read about the memory hierarchy.
Frequently Asked Questions
What is runtime storage organization in compiler design?
The run-time environment is the structure of the target computer's registers and memory that manages memory and keeps track of information required to execute a program.
What is storage allocation?
The process of connecting a storage space with a variable so that the data items represented by the variable can be recorded internally is known as storage allocation.
What is the difference between Stack and heap?
The memory space is divided into Stack space and Heap space. The stack space is mainly used to store how methods are executed and local variables. Heap memory employs dynamic allocation to allocate and deallocate memory blocks. Stack memory constantly kept blocks in LIFO(Last In First Out) order.
Conclusion
In this article, we learned about storage organization in compiler design. We learned that the runtime environment is the structure of the target computer's registers and memory that manages memory and keeps track of information required to execute a program.
To summarise, there are three types of Runtime Environments :
- Fully static
- Stack Based
- Fully Dynamic
We also learned that a storage-allocation decision is said to be static if it can be decided by the compiler based just on the program's text, rather than what the program does as it executes. On the other hand, a decision is dynamic if it can only be made while the program is running.
To support heap management, garbage collection allows the run-time system to detect useless data components and reuse their storage, even if the programmer does not explicitly return their space.
Recommended Articles
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!