【发布时间】:2023-07-10 04:12:02
【问题描述】:
#include <iostream>
using namespace std;
int screw(int x){
if(x==1)
return x;
else
screw(x-1);
}
int main(){
cout<<screw(5)<< endl;
return 0;
}
此代码的输出:1 当他们的代码的“else”部分没有返回语句时,任何人都可以解释螺丝(5),螺丝(4),螺丝(3),螺丝(2)如何返回1。 使用调用堆栈来解释将不胜感激。非常欢迎任何与它相关的基础知识的见解。
【问题讨论】:
-
首先,turn on your warnings。这是你第一次暗示出了点问题。
-
其他路径不传播返回值。 UB作为最终返回值丢失。
-
执行此代码时我没有收到任何警告。
-
似乎是一个可以用您的调试器轻松解决的问题。
标签: c++ recursion architecture callstack recurrence