Segmentation fault on returning int

Below I have my two functions which are, when when insertItem calls findIndex producing a segmentation fault. For some reason this happens when the value is returned. (I will include the cout statements so that it is easy to see exactly where this error happens). I am trying to locate the index of a value which is not in the list so -1 should be returned but never does. The output is below.

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: ... Inside insertItem function, length: 0

Entering findIndex function

In findIndex, this is middle: 0 and this is the item: Name: Eman ID: 81012 and this is the length: 0

About to exit and return value from findIndex function

findindex Function: -1 was returned

Segmentation fault (core dumped)

~$:

So even the print saying -1 was returned is hit but nothing ever gets back to the original function. I am not sure as to what could cause a seg fault in this area. Could this return do it?

1 answer

  • answered 2017-06-17 18:23 Mike Nakis

    The following loop:

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

    probably writes to 1 index past the length of the array, because valid array indexes probably range from 0 to length - 1.

    A write to an illegal memory location can corrupt the stack, and this is likely to manifest as a crash when returning from a function.

    Still, you really need to start using a debugger.