If LP recursion is not allowed, could there be cases?

Is the Stack Overflow exception related to using recursion only? In what other context can we give this exception?

+4
source share
5 answers
  • allocation of local variables that are too large to fit on the stack, for example, an array with a million elements on the 64 KB stack.
  • call stack is too deep, even without recursion, if each routine has many local variables. example: a () calls b () calls c () ... calls z () a1 () ... calls z99 (). Each local subroutine variable, as well as the return address for each function (and possibly the stack protector) remain on the stack until the stack disconnects as each function exits.
+4
source

If you allocate huge buffers on the stack using C99 dynamic arrays. i.e.

void stackkiller(int size) { char toohuge[size]; printf("Don't call this with too big an argument!\n"); } 
+3
source

Since any recursive algorithm can be turned into iterative, and vice versa, here the algorithm is in C99:

 void stackoverflow() { for (size_t n = 0; ; n *= 2) char a[n]; } 

(disable compiler optimization.)

+2
source

If you have too much memory allocated on the stack (for example, see the VS compiler ).

+1
source

It is important to note that both deep recursion and deep call chains do not fundamentally cause a stack overflow. The reason for the overflow is that each call allocates a new stack stack, thereby adding to the use of stack space.

Many languages ​​allow arbitrarily deep recursion / calls via tail call optimization. Some languages, such as ML and Haskell, will internally convert some (when the call is at the end of the calling function) functions / recursive calls to avoid using the extra stack space, which allows efficiently infinite recursion. The idea here is that if the call is at the very end of the calling function, the stack space of the calling functions is no longer required and can be fixed for use by the called function.

+1
source

All Articles