if语句导致分段错误

时间:2015-02-28 02:39:00

标签: c segmentation-fault

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

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
}

1 个答案:

答案 0 :(得分:0)

至少,让你的括号和花括号排成一行。我删除了一个大括号,并修复了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                                                     
}