Introduction to static memory allocation
In this blog we will be learning about how the memory can be allocated staticly!
it is recommended to have a decent grasp on some concepts like pointers, stack|heap memory, I’m going to assume that you are already familiar with these topics so lets dive in!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int z = 10; // global variable which is also allocated staticly
void memoryallocation()
{
static int x = 10; // memory allocated staticly using keyword "static"
int y = 10; // automatic (stack) allocated memory
x++;
cout<<"value for x is : "<<x<<endl;
//incrementing y
y++;
cout<<"value for y is : "<<y<<endl
//incremented z
z++;
cout<<"value for z is :"<<z<<endl;
}
int main()
{
for(int i = 0 ; i<3 ; i++)
{
memoryallocation();
}
}
One major difference in static and dynamic memory allocation is the amount of control we have on the allocation process of our code that takes place inside the memory
dive into the code
memoryallocation()
In this function we create 2 variables , one of them
xisstaicly allocatedinsidememoryand it is also astatic variableThen we have the variable
y, which isallocated automatically (stack)We also have a global variable
zwhich is created outside the functionmemoryallocation()and it’smemoryis alsoallocated staticlyThis function is also responsible for
incrementinganddisplayingthe values of the variables.
int main()
- we simply use a
forloop to call the function3times , now what is going to be interesting is the output
output
1
2
3
4
5
6
7
8
9
value for x is : 11
value for y is : 11
value for z is : 11
value for x is : 12
value for y is : 11
value for z is : 12
value for x is : 13
value for y is : 11
value for z is : 13
The values of
xandzare the only values which are being incremented every time and being updated , Both of the values started from10and finished at13because we called the functionmemoryallocation()3 times , They arestaticly allocatedthat is why they retain their values till the very end of theprogram itselfnot just thefunctionand when program finishes , that is when they are finallydeallocatedfrom the memoryThe value for
ydid not change , ehh why? because it is not allocated statically due to which every time the functionmemoryallocation()exits ,yis destroyed and then again when the functionmemoryallocation()is called , the value foryisreinitializedand this keeps happening that is why value forynever changes
How much control do we have?
- you might have heard that we have no control at all in
static memory allocationbut it is only partially correct because we are still in control of the memory allocation processpartially, because we can specify thedata typeand also thescopeof a variable
Allocation of Static Memory
- The memory for
staticly allocated variablesis allocated at compile time and it retain it’s value throughout the program , not just till the end of the function
Deallocation of Static Memory
- Unlike dynamic memory (allocated with
new), which must be manually deallocated withdelete, both global and static variables are automatically deallocated by the operating system when the program terminates. This means that while they exist in memory for the program’s duration, they don’t require explicit cleanup.
Relationship with the Stack
These are stored in the data segment and have a lifetime equal to the program’s duration. They are not affected by function calls and do not follow the stack’s LIFO principle.
While in the case of
automatically allocated variablesthey do follow the stack’s LIFO principle and stored on the stack and their lifetime is limited to the function call which means they arereinitializedevery time the function they are declared inside is called