首页 文章

返回int时的分段错误

提问于
浏览
-1

下面我有两个函数,当insertItem调用findIndex产生分段错误时 . 出于某种原因,在返回值时会发生这种情况 . (我将包含cout语句,以便很容易看到此错误发生的确切位置) . 我试图找到一个不在列表中的值的索引,因此应返回-1但从不这样做 . 输出如下 .

template <class ItemType>
int SortedList<ItemType>::findIndex(ItemType item) {
  cout << "Entering findIndex function" << endl;
  int first = 0;
  int last = length-1;
  int middle;
  bool found = false;
  while(!found) {
    middle = (first+last)/2;
    cout << "In findIndex, this is middle: " << middle << " and this is     the item: " << item << " and this is the length: " << length << endl;
    if(info[middle] == item) {
      cout << "In findIndex found is now true" << endl;
      found = true;
    }
    else if(item < info[middle])
      last = middle-1;
    else// if(item > info[middle])                                                                                                                                                   
      first = middle+1;
    if(first > last)//else// if(first > last)                                                                                                                                        
      break;
  }
  cout << "About to exit and return value from findIndex function" << endl;

  if(found == true) {
    cout << "findIndex Function: the index of the found value was " <<     middle << endl;
    return middle;
  }
else {
    cout << "findindex Function: -1 was returned" << endl;
    return -1;
  }
}




template <class ItemType>
void SortedList<ItemType>::insertItem(ItemType item) {
  cout << "Inside insertItem function, length: " << length << endl;
  if(findIndex(item) != -1)
    cout << "**Item already in the list" << endl;
  else if(length == Max_Items)
    cout << "**There is no room in the list" << endl;
  else {
    cout << "before the try" << endl;
    try{
      cout << "This is length at the start of the insertItem function: " << length << endl;
      if(length == 0) {//if the list is empty item becomes the first item in the list                                                                                               \

        cout << "This is right after length==0 in insertItem function" << endl;
        info[0] = item;//was this->inf...                                                                                                                                            
        length++;
        cout << "This is length right after incrementing it up" << length << endl;
      }
      else {//its not the first item in the list                                                                                                                                     
        for(int i = 0; i <= length; i++) {
          cout << "This is the length and i respectively right inside the for in insertItem" << length << " " << i << endl;
          if(i == length) {
            cout << "this is where i == length" << endl;
            info[i] = item;
            length++;
            break;
          }

          if(info[i] < item)
            continue;
          //inserting the item where it needs to go                                                                                                                                  
          for(int p = length; p > i; p--) {//was >=                                                                                                                                  
            info[p] = info[p-1];
          }
          //item = info[i];                                                                                                                                                          
          info[i] = item;
          length++;
          break;
        }
      }
    }catch(...) {cout << "**insertItem failed" << endl;}
  }
  cout << "This is length at the end of the insert item function: " <<     length << endl;
}

output:... insertItem函数内部,长度:0

输入findIndex函数

在findIndex中,这是中间:0,这是项目:名称:Eman ID:81012,这是长度:0

即将从findIndex函数退出并返回值

findindex函数:返回-1

分段故障(核心转储)

〜$:

因此即使返回-1的印刷品也会被击中,但是没有任何东西可以回到原来的功能 . 我不确定在这个区域可能导致seg故障的原因 . 这回报可以吗?

1 回答

  • 0

    以下循环:

    for(int p = length; p > i; p--) {
        info[p] = info[p-1];
    

    可能写入超过数组长度的1个索引,因为有效数组索引的范围可能从 0length - 1 .

    写入非法内存位置可能会损坏堆栈,这可能会在从函数返回时显示为崩溃 .

    不过,您确实需要开始使用调试器 .

相关问题