Static memory is a part of the entire memory segment in coding. The compiler utilizes this space to store variables and code. Static memory is organized whereas heap is unorganized. Both have their pros and cons.
Memory Representation
This is just for visualization. Take a look …
This blog will focus on static memory allocation. (The stack part)
Static Memory
Static memory denotes when the memory allocation is static. Meaning, the memory size does not vary. It is decided during the compile-time or before the run-time.
What is static?
The above picture represents an array that holds the datatype integers. The used space is up to index 3 and the following indices are empty. Yet, there is no way to delete the remaining indices.
Take another scenario where you need more space to store values. You cannot resize the array to hold more values.
This is static. Once the size is determined, it cannot be resized again. But that’s not the case in Heap Memory Allocation.
More about Stack
Before going to an actual example where we analyse the memory allocation, there are few terms you need to know
Stack
Stack is a type of arrangement where the first one becomes the last accessible one.
This picture has 4 diaries. The colours are red, brown, peach and blue. To create this, the first diary kept should have been the red diary. And on top of it should be the brown and then the peach. The last diary is the blue diary which is the completed structure that we see in the picture.
Now, how to access this structure? The last diary kept (i.e the blue diary) is the first accessible diary in the structure. Then the peach diary and the brown diary and finally the red diary.
This concept is known as LIFO. Last In First Out.
Stack Frame
Stack frame or Activation Record is a segment of a memory allocated for a section of the code.
When the variable or the segment goes out-of-scope, then the activation record is deleted.
Let’s analyse a code
#include<stdio.h>
void func1(int i) { int a; }
void func2() { int x = 30; func1(x); }
void main() { int a; float b; func1(); }
The above code is the example that I will use to illustrate the static memory allocation.
The code is written in C Language. The code might look illogical as it does pretty much nothing useful. This is just for demonstration purposes.
The execution of the code starts with the main function that in the third line. Here, an activation record is created for the main segment which consists of the integer a and the float b.
Next, func1( ) is called. Activation record for main( ) still exists and the activation record for func1( ) is created. Here the variable x of type integer is allocated in the segment.
Then, func2( ) is called. Activation record for main( ) and func1( ) still exists and the activation record for func2( ) is created. Here the argument variable i and the inner variable a, both of type integer are allocated in the segment.
When func2( ) ends, the activation record is deleted. But func1( ) and main( ) still exists.
When func1( ) ends, the activation record is deleted. But main( ) still exists.
When main( ) ends, the activation record is deleted.
Now, the same 6 steps are demonstrated in the picture below.
This is the reason for the topic Scopes. Local scope, Global scope can be understood when you learn and understand this topic.
Now, you can also see that the last created activation record is the first to be deleted (i.e go out of scope). This is why that memory portion is named Stack.
If you have read up to this and understood the topic, that means you can read and explore the world of memory allocation. You can understand topics like backtracking, storage classes and more.
It was very informative!.
Great effort Grace!