Why Does StackOverflows ?🤔
If you have just started programming, or you are quite good at it. then you must have encountered the question “Why does Stack Overflows?” and what is the actual reason for it?
if you are a university student then you have been given the reason that “STACK CANNOT HANDLE THE MEMORY GENERATED blah blah blah….”
( 😅 they are correct no offense)
but the questions below are not answered :(
What thing in a program generates this unmanageable memory? and how does it generate this much memory?
Let’s find it out 🔍
wait….wait … before finding it out we need to understand how a machine divides the program or code written by a programmer? which will help us to find out the reason for Stackoverflow.
Note: for simplicity, I have considered C++ syntax for this article.
When a program is compiled by a compiler, the resulting executable file is divided into four segments :
· Code
· Data
· Heap
· Stack
From these segments the size of Code, Data, and Stack is decided at compile time i.e. they are of fixed size, and size of the heap if defined at runtime.
Code Segment
The code segment contains the program's executable code, also known as the text segment. It contains the instructions that are executed by the processor, such as functions and instructions for data manipulation.
Data Segment
The data segment contains initialized global and static variables, as well as constants. This segment is read-write. The data segment is further divided into two sub-segments: the initialized data segment and the uninitialized data segment. The initialized data segment contains initialized global and static variables, while the uninitialized data segment contains uninitialized global and static variables.
Heap Segment
The heap segment is used for dynamic memory allocation. It grows upwards in memory and is managed by the programmer. The heap is typically used for data structures that need to be resized dynamically at runtime.
Stack Segment
So how stack is utilized in program?
The stack segment is used to store function parameters, local variables, and the return address of a function. It grows downwards in memory and is managed by the system. When a function is called, the necessary stack space is allocated, and when the function returns, the stack space is deallocated.
Now what does this mean?
As the program is divided into segments by compiler, and each of these segments is given logical memory addresses (not actual addresses from the hard disk).
So when the program is getting executed at that time IP needs to go from one segment to another segment. But why?
Get Nisarg Pipaliya.’s stories in your inbox
Join Medium for free to get updates from this writer.
The execution of the program starts from the code segment, and the variable is in the data segment and the variables which are assigned memory dynamically are in the heap, so when execution starts from the code segment and when the program needs to make a jump to data or heap segment for accessing variable or function stored their then it stores the segment address and instruction pointer(IP) for the next instruction after the function call on the stack. now IP can move to that segment performs the operation and then come back to the code segment and continue the execution.
Fine, so this is the end of the article. Meet you next time :)
Hey wait what fine? What are these segment address and instruction pointer? 😑
The segment address is simply the address assigned to the segment (code, data, heap, stack) the logical address(it means that not the actual address from the hard disk ).
And the instruction pointer is nothing but it is the pointer that contains the logical address of the instruction currently being executed.
So consider the below written C++ program.
#include<iostream> // ignoring this and below line as they are instruction
// for compiler for bringing necessary functions from
//iostream library .
using namespace std; // Yes, I am ignoring you.
void My_fav_function() // assume logical address (LA) 1
{ // LA=2
cout<<”This my favourite function “<<endl; // LA=3
} // LA=4
int main(void) // LA=5
{ // LA=6
cout<<”Hey there”<<endl; // LA=7
cout<<” Why does stack overflows?” <<endl; // LA=8
My_fav_function(); //LA=9
cout<<”Well stack works very hard for execution of the program 😊”<<endl;//LA=10
return 0; //LA=11
} //LA=12 -> end of program.So now whenever the program is executed the instruction pointer is assigned the value of the “MAIN” method.
And hence the execution starts from the main method so let’s trace the value of this instruction pointer (assuming that the program was started at time T=0)
TIME | Value of Instruction
| Pointer
______________________
0 | 5 (refers to LA=5)
1 | 6
2 | 7
3 | 8
4 | 9Now, at this point we are calling “My_fav_function()” therefore we need to assign the instruction pointer the value of the address where this function is stored, so let's do it what is the problem with this?
The problem is that, if we assign the address of the function are continue the execution then whenever the function gets completed the IP would not know where to go from here. Or how do I get back to the main function, and continue the execution?
Who will save IP from this catastrophe 😭?
So here the stack would store the value of the segment address(mentioned above) and instruction pointer onto it and whenever the “MY_fav_function()” gets over the stack pops the address stored and assign it to IP so, now
The tables continue as:
TIME | Value of IP
______________________
5 | 1
6 | 2
7 | 3
8 | 4 (now stack will pop the value and assign IP the address of the next instruction which is after function call.)
9 | 10
10 | 11
11 | 12So, here when the functions are called the address is stored on the stack.
Now when we have a recursive function and the depth ( i.e. how many times a function is called) becomes huge i.e. it generates a huge recursive tree the stack fails to store all these values of IP and runs out of space, so this phenomenon of running out of space while trying to store the value of this instruction pointers is called StackOverflow (well this is how I see it.)
Into the comment section:
Which of this two code will generate stackoverflow?
code 1
while(true){
cout<<”Hii”<<endl;
}code 2
funct(){
cout<< “you have been caught into infinite loop dormammu” <<endl;
funct();
}Hope you liked this article 😊 and if you liked it please give it kudos.
You can mention the topics on which you would love to read articles in the comment section.
🔍 Keep exploring 🔍
Note: the concept of the instruction pointer, segment address and how the stack deals with function calls is a wonderful topic in 8086 Microprocessor programming. here In the example discussed I have considered only IP but in actual processing, it is somewhat different. I have not discussed it here as it is a deep concept and while programming you may not need these deep concepts of how a processor works.










