Stack overflow caused by recursive function

后端 未结 6 1312
面向向阳花
面向向阳花 2020-11-27 20:25

I\'m a beginner in C++. Yesterday I read about recursive functions, so I decided to write my own. Here\'s what I wrote:

int returnZero(int anyNumber) {
    if         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 20:41

    Any "boundless" recursion, that is recursive calls that aren't naturally limited to a small(ish) number will have this effect. Exactly where the limit goes depends on the OS, the environment the function is called in (the compiler, which function calls the recursive function, etc, etc).

    If you add another variable, say int x[10]; to your function that calls your recursive function, the number needed to crash it will change (probably by about 5 or so).

    Compile it with a different compiler (or even different compiler settings, e.g. optimization turned on) and it will probably change again.

提交回复
热议问题