catching segmentation faults?

Hi, so I was writing a linked list that stores a character in each node and links them all together to write strings. What I was testing for in the main that I wrote was to check to see if my out of bounds conditional was working - by calling an index out of bounds in my string. So far everything works when I access an index within bounds, but when I access an index out of bounds it does exactly as it is defined below - prints me a message saying I'm out of bounds.

But it also gives me a segmentation fault, which I know, is because I'm accessing memory I don't have, but was there a way to catch this segmentation fault so it doesn't halt my program? Because I have other testcases in my main below this one that I want to test, but this segmentation fault is crashing everything before it can get to them.

And simply rearranging the test cases so that the segmentation fault is the last testcase tested is not an option.

1
2
3
4
5
6
7
8
9
10
11
12
  char & operator [] ( const int index )
    {
      if(!inBounds(index))
      {
        cout << "out of bounds!" << endl;
      }
      ListNode *iter = head; //you forgot to set iter equal to head you dingus
      for(int i = 0; i < index; i++)
        {iter = iter->next;}
      return iter -> info;

    }
Where are you crashing? line 3, or line 9?

If you're crashing at line 3, there's a problem with your inBounds() function.

Line 9: How do you indicate end of list? iter->next == NULL ? If so, I suspect you're iterating past the end of the list.

Since you haven;t provided the source for your inBounds(), it's impossible to tell whether or not that's working correctly.



Yeah I was, I switched around the format of my function so that IF the index is inBounds execute the for loop first, else print out of bounds. I'm no longer getting a segmentation fault but for some reason it's still trying to print something from the index I'm calling out of bounds. I'm not sure if I like that it does that, I'd rather just have it print that it's out of bounds and leave it at that.

1
2
3
4
5
6
7
8
9
10
11
12
char & operator [] ( const int index )
    {
      if(inBounds(index))
      {
        ListNode *iter = head; //you forgot to set iter equal to head you dingus
        for(int i = 0; i < index; i++)
          iter = iter->next;
        return iter -> info;
      }
      else if(!inBounds(index))
        cout << "out of bounds!" << endl;
    }//inBounds 


EDIT : forgot to provide inBounds

1
2
3
4
5
6
7
8
private:
    bool inBounds( int i )
    {
      return (i >= 0) && (i < length());
    }

static int length(ListNode * L) //might as well provide length just in case
      { return L == 0 ? 0 : 1 + length(L -> next); }
Last edited on
1st snippet:
line 10: There's no reason for the if (! inBounds(index)) here. You're in the scope of the else, that is the only possibility.

line 12: You don't supply a return statement, so the compiler will supply a return 0 for you. Obviously, that's not a valid char reference. You best course of action would be to throw an exception here. You don't want to return to the caller, letting the caller think he has a valid character reference.

2nd snippet:
line 4: length() is being called with no argument. This is either a different function than the one at line 7, or a typo.

line 8: You do check to see if L is NULL. I'd be suspicious that next isn't always getting set to NULL for the last node.

Last edited on
> You don't supply a return statement, so the compiler will supply a return 0 for you.
That's only valid for `main()'
In this case is undefined behaviour.
If I throw an exception like
 
throw std::invalid_argument("Out of bounds!");


wouldnt that just abort my main? Couldn't I just return the null character?

EDIT: er, I probably wont be able to return the null char, or at least I haven't figured out how to do it properly yet, I'll see if I can find out how.
Last edited on
wouldnt that just abort my main?

It will if you don't catch said exception.


Couldn't I just return the null character?

You're returning a reference to char. If you return a reference to a null character, you must ensure that null character lives after the function returns.
Topic archived. No new replies allowed.