3

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?

  • Stack overflow may occur at the calculation process of the compiler. I don't think typical compilers do the calculation. – MikeCAT Feb 25 '21 at 16:02
  • Well, how much stack memory do you have? This can be different. Also, the compiler knows the size of each stack frame, but not how many frames you might have. Consider a recursive algorithm, or memory dynamically allocated on the stack. – Ted Klein Bergman Feb 25 '21 at 16:03
  • 1
    A computer only has so much "stack space". If you use more then that in your program for some reason (infinite recursion, writing ou of bounds of an array, other undefined behavior), then you get a stack overflow – NathanOliver Feb 25 '21 at 16:04
  • related/dupe: https://stackoverflow.com/questions/26158/how-does-a-stack-overflow-occur-and-how-do-you-prevent-it and https://stackoverflow.com/questions/12146201/how-to-handle-or-avoid-a-stack-overflow-in-c – NathanOliver Feb 25 '21 at 16:05
  • ***If the compiler calculates the size of the stack required before runtime*** It really can't do that in all but the simplest case. – drescherjm Feb 25 '21 at 16:06
  • Compiler does not calculate size of stack, why would it? Size of available stack memory can be changed from one run to another. – Yksisarvinen Feb 25 '21 at 16:07
  • ***At compile time does the compiler calculate the total amount of memory required for stack of the whole program?*** No it does no do that and in the general case it is impossible to do so without executing the program with the data set. – drescherjm Feb 25 '21 at 16:11
  • 3
    The answer to the question "If my hypothesis is correct, why do the facts contradict my hypothesis?" is usually that the hypothesis is incorrect. – molbdnilo Feb 25 '21 at 16:33

3 Answers3

5

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.

463035818_is_not_a_number
  • 64,173
  • 8
  • 58
  • 126
  • My question is, is there a preventative procedure for these exception cases? Maybe allocation to Heap? – Streamline Astra Feb 25 '21 at 16:15
  • 1
    @StreamlineAstra But no, you can't prevent stack overflows as you often can't determine which functions are getting called in which order. Well, in theory, you can, but a program flow determined by unknown events makes it difficult. – Ted Klein Bergman Feb 25 '21 at 16:18
4

Consider this very simplified example function (pseudocode)

 void Foobar(long nested) {
    if( --nested >= 0)
        Foobar(nested);
 }

Each call pushes (at least) the return address onto the stack.

Question: How much memory do you need for this?

Exactly. That's the reason.

Ch3steR
  • 15,132
  • 4
  • 20
  • 45
JensG
  • 12,102
  • 4
  • 40
  • 51
3

The compiler will often know how much stack memory each function will require, but cannot know at compile time which functions are getting called. So you might get a stack of function calls that exceed the stack memory.

Also, stack memory can be changed between processes or threads, so the compiler don't know the limit at compile time.

Lastly, you can dynamically allocate memory on the stack. This is done through recursion or with calls like alloca in C.

Ted Klein Bergman
  • 7,245
  • 3
  • 20
  • 40