Why are stackoverflow errors chaotic?

后端 未结 7 879
醉话见心
醉话见心 2021-01-12 11:29

This simple C program rarely terminates at the same call depth:

#include 
#include 

void recursive(unsigned int rec);

int ma         


        
7条回答
  •  梦谈多话
    2021-01-12 12:07

    Your program runs infinitely as there is no base condition in your recursive function. Stack will grow continuously by each function call and will result in stack overflow.
    If it would be the case of tail-recursion optimization (with option -O2), then stack overflow will occur for sure. Its invoke undefined behavior.

    what would influence the available stack size so that the stackoverflow does not always occur at the same call depth?

    When stack overflow occurs it invokes undefined behavior. Nothing can be said about the result in this case.

提交回复
热议问题