我遇到了一些奇怪的C行为。我有一个数组(我标记为SHOULD_NOT_CHANGE)在一个看似无关的函数调用之后发生了变化。我的数组不是全局的,函数调用不包括特定的数组。如果有人能弄清楚发生了什么,我将非常感激。这是主要的:
Error in ans[!test & ok] <- rep(no, length.out = length(ans))[!test & :
replacement has length zero
In addition: Warning message:
In rep(no, length.out = length(ans)) :
'x' is NULL so the result will be NULL
这是罪魁祸首:
int main(){
int generate4[16] = {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int propagate4[16] = {0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0};
int SHOULD_NOT_CHANGE[4] = {0, 1, 0, 0};
int carry_in16[4] = {1,2,1,1};
int carry_in4[16] = {0};
printf("\nShould be 1: %d", SHOULD_NOT_CHANGE[0]);
printf("\nShould be 0: %d", SHOULD_NOT_CHANGE[1]);
printf("\nSHOULD NOT CHANGE : ");
print_binary_array(SHOULD_NOT_CHANGE, 4);
// Here is where the problem is
carry(propagate4, generate4, carry_in4, carry_in16, 64, 4);
printf("\n\nShould be 0: %d", SHOULD_NOT_CHANGE[0]);
printf("\nShould be 1: %d", SHOULD_NOT_CHANGE[1]);
printf("\nSHOULD NOT CHANGE : ");
print_binary_array(SHOULD_NOT_CHANGE, 4);
return 0;
}
这是我的输出:
void carry(int* p, int* g, int* carry_in, int* group_carry, int size, int block)
{
for (int j = 0; j < block; j++){
carry_in[j] = g[j] + p[j] * group_carry[j];
for (int i = 1; i < block*4; i++){
carry_in[4*j+i] = g[4*j+i] + p[4*j+i] * group_carry[j];
}
}
}
答案 0 :(得分:1)
您正在索引超过函数carry_in
中carry
的范围。 block
为4,因此4*block
为16.因此j
从0到3,i
从1到15运行。因此4*j+i
可以是大到12 + 15 = 27,方式大于最大安全值15。