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 [] ( constint 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;
}
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 [] ( constint 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;
}
elseif(!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());
}
staticint length(ListNode * L) //might as well provide length just in case
{ return L == 0 ? 0 : 1 + length(L -> next); }
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.
> 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.
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.
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.