使用静态变量的递归调用的不同输出
Different output on recursive call using static variable
int fun1(int x){
static int n;
n = 0;
if(x > 0){
n++;
return fun1(x-1)+n;
}
return 0;
}
int fun(int x){
static int n = 0;
if(x > 0){
n++;
return fun(x-1)+n;
}
return 0;
}
谁能告诉我乐趣和乐趣之间的区别1? 获得不同的输出!!
static int n = 0;
是一次性初始化
就像下面的片段一样,
bool initialized = false;
static int n;
int fun1(int x){
if(!initialized){
n = 0;
initialized = true;
}
if(x > 0){
n++;
return fun1(x-1)+n;
}
return 0;
}
static int n; n =0
在每次递归调用时重置为零。像下面一样,
bool initialized = false;
static int n;
int fun(int x){
if(!initialized){
n = 0;
initialized = true;
}
n = 0;
if(x > 0){
n++;
return fun(x-1)+n;
}
return 0;
}
实际上,n
是 .BSS 并在加载时初始化为零。
在fun1
中,n
设置为每次调用函数时0
。
在fun
中,n
被初始化为在程序启动时0
,但此后只在n++
更新。
相关文章:
- 当使用通配符和null指针调用函数时,对输出的说明
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 使用静态变量的递归调用的不同输出
- cmake add_custom_command + Xcode:多输出 = 多命令调用
- 调用 cout 时如何在结构中输出常量文本?
- 我的 cout 上有一个奇怪的输出,它把答案放在第一位,然后在我调用它的地方放一个奇怪的输出.我该怎么办?
- 函数调用导致没有输出
- 使用 Node.js N-API 调用 C 函数时,不会显示预期的输出
- 如何调用继承的重载运算符<<并在派生类的输出中添加更多文本?
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 当我从头文件和实现文件调用我的函数到我的主文件时,我没有得到任何输出
- 空指针(为缺少的输出数组调用的 create() 在 create,文件 /home/m/OpenCV/modules/
- 为什么在杂注 omp 关键之后多次调用 printf 会产生乱码输出?
- std::cout 如果从自定义分配器 (Visual Studio 2019) 调用,则不会输出
- 将 Linux system() 调用命令的输出重定向到仅可变的
- 干净取消在输入/输出调用中被阻止的 std::线程
- 不同的输出调用clang上的malloc应用编译器选项-00和-03
- 从缺少 printf 语句输出C++调用 C API
- 如何使用 Matlab 的多个输出调用C++函数
- 无法使用重定向标准输出调用 g++