[英]Bus Error when using recursion
So, I'm using recursion and going through an array named code which contains integers and a value is computed after traversing through the array. 因此,我正在使用递归,并遍历一个名为code的数组,该数组包含整数,并且在遍历数组后会计算出一个值。 But I don't know for what reason this is giving a BUS ERROR : 10. I even tried manually going through the array and everything seems fine, array is sufficiently big.
但是我不知道这是什么原因导致产生BUS错误:10.我什至尝试手动遍历数组,但一切似乎都很好,数组足够大。
//change to bits/stdc++.h
#include "stdc++.h"
#define MAX 5001
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
int code[MAX];
int co_code;
ull rec(int i, int j, int k){
if(k==j-1)
return 0;
if(i==j-1)
return (rec(k+2, j, k+2));
int temp = code[i]*10 + code[i+1];
if(temp<=26)
return (1 + rec(i+1, j, k));
else
return (rec(i+1, j, k));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while(1){
memset(code, 0, sizeof(code[0])*MAX);
string str;
cin>>str;
size_t size = str.length();
co_code = 0;
while(co_code!= size){
code[co_code] = str[co_code] - '0';
co_code++;
}
if(code[0] == 0)
break;
cout<<rec(0, co_code-1, 0) + 1;
}
return 0;
}
The error is in accessing the array outside the size given by co_code, so it can be fixed by changing the if condition. 错误在于访问超出co_code给定大小的数组,因此可以通过更改if条件来解决。
Corrected : - 已更正:-
if(k>=j-1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.