Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stack overflow at runtime

Tags:

c++

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?

like image 239
Streamline Astra Avatar asked Sep 05 '25 03:09

Streamline Astra


1 Answers

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.

like image 167
463035818_is_not_a_number Avatar answered Sep 07 '25 23:09

463035818_is_not_a_number



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!