2017-06-17 93 views
-1

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

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; 
} 

输出: ... 内部insertItem功能,长度:0

输入findIndex功能

在findIndex,这是中间位置:0,这是该项目:名称:林二汶ID :81012,这是长度:0

从findIndex函数关于退出和返回值

findindex功能:-1返回

分割故障(核心转储)

〜$:

说着-1返回即使打印被击中,但从来都没有变回原来的功能。我不确定在这个区域会发生什么可能导致seg故障。这个回报能做到吗?

+3

解决此类问题的正确工具是您的调试器。在*堆栈溢出问题之前,您应该逐行执行您的代码。如需更多帮助,请阅读[如何调试小程序(由Eric Lippert撰写)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,你应该[编辑]你的问题,以包含一个[Minimal,Complete,and Verifiable](http://stackoverflow.com/help/mcve)例子来重现你的问题,以及你在调试器中所做的观察。 –

+0

'findIndex'不处理空的容器。 – Rabbid76

+0

严重的是,您使用的是模板,无法使用调试器来调试自己的代码?这是一场灾难。学习使用调试器,因为它基本上是一项要求,因为你正在做的事情具有先进性。 – PaulMcKenzie

回答

0

以下循环:

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

大概写入1个索引经过阵列的长度,因为有效阵列索引可能范围从0length - 1

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

仍然,你真的需要开始使用调试器。

+0

我从现在开始意识到这一点。这不是发生seg故障的地方。我发布这个的原因是seg故障发生的地方,我只是无法调试它的位置。它把我扔了 –

+1

@MichaelSmith *这不是发生seg故障的地方* - 但可能是灾难发生的地方。与大多数其他计算机语言不同,只要你犯这样的错误,C++不会弹出消息框。该循环会破坏内存,并且内存损坏会导致未定义的行为。 – PaulMcKenzie

+0

@PaulMcKenzie它不可能在我的情况下发生这种情况,因为p必须大于i意味着p至少为1,当p至少为1时,可以访问的最低索引是0(1-1 = 0) –