0
Can i know what is stack overflow in c?explain it in simple words pls
2 Answers
+ 1
I like Austin's answer :) but the Stack Overflow website is actually named after the very real event - the crash you will experience if your stack overflows.
The stack is an area in memory reserved for functions and their variables while they are being executed. It can grow "up" or shrink "down" depending on the level of nested function calls.
when funcA calls funcB it gets "pushed" onto the stack (and the stack grows) and when funcB returns it gets "popped" off the stack.
So the stack is a bit like a "dagwood" sandwich, where every layer represents a function in the chain of nested function calls currently being executed. Something like this:
void funcD() {
//Here stack is CRuntime|main|funcA|funcB|funcD
}
void funcC() {
//Here stack is CRuntime|main|funcA|funcB|funcC
}
void funcB() {
//Here stack is CRuntime|main|funcA|funcB
funcC();
funcD();
}
void funcA() {
//Here stack is CRuntime|main|funcA
funcB();
}
void main() {
//Here stack is CRuntime|main
funcA();
}
So how do you overflow your stack - by simply too many nested calls function calls that exceed the maximum stack space allowed by the OS. The following (recursive) program should do it:
void foo(){
long x[10000];
foo();
}
int main() {
foo();
return 0;
}
0
stack is simply a memory space where the variables you create gets stored.
when the stack is full, it is called a stack overflow condition.