If the compiler calculates the size of the stack required before runtime, how does a stack overflow occur? At compile time does the compiler calculate the total amount of memory required for stack of the whole program?
No.
A compiler can only know what happens during runtime to some very limited extend. For example it cannot tell how much stack is needed to execute the following function:
void foo() {
int x = 0;
std::cin >> x;
if (x == 42) foo();
}
Stack is a limited resource and if you exceed it you get an stackoverflow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With