#include<stdio.h>
int recursive(int f,int g){
static int a;;
static int b;
int c = 100;
a = f;
b = g;
if(c != 105){
a++;
b++;
c++;
recursive(a,b);
}
printf("\n a : %d b : %d \n",a,b);
return 0;
}
int main(){
int a = 10;
int b = 1;
recursive(a,b);
}
上面的示例递归程序给出了段错误。无法理解为什么会发生段错误,因为不涉及任何指针。
最佳答案
你在那里有无限递归。因为c
从不等于105
(每次进入函数时都设置为100
),函数只会一遍又一遍地调用自己,直到你炸毁堆栈(超出其容量)。
它归结为一些简单的事情:
int blowUpStack (int a) {
blowUpStack (a);
}
关于c - 具有递归功能的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25569948/