gpt4 book ai didi

c - if 语句导致段错误

转载 作者:行者123 更新时间:2023-11-30 19:44:04 25 4
gpt4 key购买 nike

我正在编写一个缓存模拟器,由于某种原因,我的 if 语句不断出现段错误。出于调试目的,我添加了一些打印语句。所有内容都完全按照我期望的方式打印,直到 if 语句之前,然后它只是返回段错误

int find_set(node** cache, unsigned set_i, long t, int E) {
printf("finding set\n");
int i; // index
for (i = 0; i < E; i++) {
printf("trying seti = %d and i = %d \n", set_i, i);
printf("cache[set_i][i]->valid_bit = %d\n", cache[set_i][i]->valid_bit);
printf("cache[set_i][i]->tag = %d\n", cache[set_i][i]->tag);
if (cache[set_i][i]->valid_bit == 1) && ((cache[set_i][i]->tag) == t) {
printf("found tag! \n");
cache[set_i][i]->LRU_count = 0; // just used
return i; // index of the set we found
}
}
return -1; // not found
}

最佳答案

至少,让括号和大括号对齐。我去掉了一端大括号,并修复了 if 后面的括号。尝试用这个替换你的函数:

int find_set(node** cache, unsigned set_i, long t, int E){
printf("finding set\n");
int i; //index
int val;
for (i = 0; i < E; i++){
printf("trying seti = %d and i = %d \n",set_i,i);
printf("cache[set_i][i]->valid_bit = %d\n",cache[set_i][i]->valid_bit);
printf("cache[set_i][i]->tag = %d\n",cache[set_i][i]->tag);
if ((cache[set_i][i]->valid_bit == 1) && (cache[set_i][i]->tag == t)){
printf("found tag! \n");
cache[set_i][i]->LRU_count = 0; //just used
return i; //index of the set we found
}
}
return -1; //not found
}

关于c - if 语句导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28777497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com