返回 int 时的分段错误



下面我有两个函数,当插入项目调用时,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;
}

输出:...内部插入项目功能,长度:0

输入查找索引函数

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

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

查找索引函数:返回 -1

分段故障(核心转储(

~$:

因此,即使显示返回 -1 的打印也被击中,但没有任何内容会回到原始函数。我不确定是什么可能导致该区域的赛格故障。这次回归能做到吗?

以下循环:

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

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

写入非法内存位置可能会损坏堆栈,这很可能表现为从函数返回时崩溃。

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

相关内容

  • 没有找到相关文章

最新更新