I am getting a strange segmentation fault in my program. Dlist is a class that creates a linked list with operations to insert and remove items from a dynamic list. I am positive my implementation of this class is correct, but this code is producing a seg fault. The strange part is, when I make my atleastTwo and atleastOne functions pass by reference, the seg fault disappears and everything compiles. Can anyone shed some light on this problem?
bool atleastTwo(Dlist<double> stack){
try{
stack.removeFront();
stack.removeFront();
} catch(emptyList e){
cout << "Not enough operands\n";
return false;
}
return true;
}
bool atleastOne(Dlist<double> stack){
try{
stack.removeFront();
} catch(emptyList e){
cout << "Not enough operands\n";
return false;
}
return true;
}
void processInput(inputs usrInput, Dlist<double> &stack){
switch(usrInput){
case i_add:
if(atleastTwo(stack)){doOperation(stack, add);}
break;
case i_subtract:
if(atleastTwo(stack)){doOperation(stack, subtract);}
break;
case i_multiply:
if(atleastTwo(stack)){doOperation(stack, multiply);}
break;
case i_divide:
if(atleastTwo(stack)){doOperation(stack, divide);}
break;
case i_negation:
if(atleastOne(stack))negation(stack);
break;
case i_duplicate:
if(atleastOne(stack)){duplicate(stack);}
break;
case i_reverse:
if(atleastTwo(stack)){reverse(stack);}
break;
case i_print:
if(atleastOne(stack)){print(stack);}
break;
case i_clear:
clear(stack);
break;
case i_printAll:
printAll(stack);
break;
default:
break;
}
}
T *removeFront();
// MODIFIES this
// EFFECTS removes and returns first object from non-empty list
// throws an instance of emptyList if empty
Thanks
Regarding the question itself I do not see how a segfault could occur from your code. I suspect the issue being in the code for Dlist, maybe a badly implemented destructor?
To solve your problem you could implement a count of elements inside Dlist and check for it. But maybe you are not allowed to modify Dlist. The preferred solution to avoid jumpy code and too much tests would be following suggestion. Instead of testing for the amount of operands just try it, and put the exception handler inside your processing method. Problem with this second solution is that the stack might remain in an inconsistent state: that means you cannot proceed with computations and should restart from the beginning.
I suppose the latter is a way to go, you could keep a copy of your stack and make the computation on one copy. The performance would be much better this way and above that the code easier to read and understand.
I hope it helps.