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
x
isstaicly allocated
insidememory
and it is also astatic variable
Then we have the variable
y
, which isallocated automatically (stack)
We also have a global variable
z
which is created outside the functionmemoryallocation()
and it’smemory
is alsoallocated staticly
This function is also responsible for
incrementing
anddisplaying
the values of the variables.
int main()
- we simply use a
for
loop to call the function3
times , 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
x
andz
are the only values which are being incremented every time and being updated , Both of the values started from10
and finished at13
because we called the functionmemoryallocation()
3 times , They arestaticly allocated
that is why they retain their values till the very end of theprogram itself
not just thefunction
and when program finishes , that is when they are finallydeallocated
from the memoryThe value for
y
did not change , ehh why? because it is not allocated statically due to which every time the functionmemoryallocation()
exits ,y
is destroyed and then again when the functionmemoryallocation()
is called , the value fory
isreinitialized
and this keeps happening that is why value fory
never changes
How much control do we have?
- you might have heard that we have no control at all in
static memory allocation
but it is only partially correct because we are still in control of the memory allocation processpartially
, because we can specify thedata type
and also thescope
of a variable
Allocation of Static Memory
- The memory for
staticly allocated variables
is 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 variables
they do follow the stack’s LIFO principle and stored on the stack and their lifetime is limited to the function call which means they arereinitialized
every time the function they are declared inside is called