Segmentation fault error

Following is the main.cpp and infix.cpp (implementation file of my infix calculator). I have created an infix calculator using CMAKE utility so that's why there are different .cpp files.
I was using templates and for some reason I was getting seg fault error.

Infix.cpp


Infix::Infix() {
string str;
}

void Infix::setInfix(string input) {
str = input;
}

string Infix::getInfix() const{
return str;
}

int Infix::eval() {
char ch = '&';
LinkedStack<char> operatorStack;
LinkedStack<int> valueStack;

for (unsigned int i = 0; i < str.length(); i++) {//each character ch in infixExpression)
ch = str[i];
if (isOprnd(ch)) {
valueStack.push(conToInt(ch)); //Push ch converted to an int onto valueStack
}
else if (ch == '(') { //ch is a left parenthesis)
operatorStack.push(ch); //Push ch onto operatorStack
}
else if (isOpt(ch)) { //is an operator)
if (operatorStack.isEmpty() || prec(ch) > prec(operatorStack.peek())) { //is empty OR precedence of ch is greater than precedence of operator on top of operatorStack)

operatorStack.push(ch); //Push ch onto operatorStack
}
else {
while (!operatorStack.isEmpty() && prec(ch) <= prec(operatorStack.peek())) {//is NOT empty AND precedence of ch is less than or equal to precedence of operator on top of operatorStack)

execute(valueStack, operatorStack); //Execute
}
operatorStack.push(ch); //Push ch onto operatorStack
}
}
else if (ch == ')') { //ch is a right parenthesis)
while (operatorStack.peek() != '(' ) { //top of operatorStack is not a left parenthesis)
execute(valueStack, operatorStack); //Execute
}
operatorStack.pop(); //Pop the operatorStack // To remove the left parenthesis
}
}

while (!operatorStack.isEmpty()) { //operatorStack is not empty)
execute(valueStack, operatorStack); //Execute
}
return valueStack.peek(); //the top of valueStack

}

bool Infix::isOpt(char m) {
bool optr = false;
if (m == '+'|| m =='-'|| m == '*'|| m == '/')
optr = true;
return optr;
}

bool Infix::isOprnd(char m) {
bool oprnd = false;
if (m == '1' || m == '2' || m == '3' || m == '4' || m == '5' || m == '6' || m == '7' || m == '8' || m == '9' || m == '0')
oprnd = true;
return oprnd;
}

int Infix::conToInt(char m) {
int i = m;
i = i - 48;
return i;
}

void Infix::execute(LinkedStack<int>& valueStack, LinkedStack<char>& operatorStack) {
int operand2 = valueStack.peek();
valueStack.pop();
int operand1 = valueStack.peek();
valueStack.pop();
char optr = operatorStack.peek();
operatorStack.pop();
if (operand2 == 0 && optr == '/'){
throw DivByZero("Infinite Result - Cannot Handle Div By Zero");}
valueStack.push(result(operand1, optr, operand2));
}

int Infix::prec(char m) {
int i;

if(m == '(')
i = 0;

else if (m == '*' || m == '/')
i = 2;

else
i = 1;

return i;
}

int Infix::result(int operand1, char optr, int operand2) {
int result = 3;
switch (optr) {
case '*':
result = operand1 * operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
result = operand1 + operand2;
} return result;
}

main.cpp

#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>

using namespace std;

#include "Infix.h"

int main() {

ifstream instream;
string expression;
char input = 'n';
Infix in;
char answer;

do{
cout << "select f for file input or anything else for a string input";
cin >> answer;

if (answer == 'f') {

cout << "Enter a file name: " << endl;
cin >> expression;
instream.open(expression);

if (!instream)
{
cout << "Error opening file \n";
exit(1521);
}
while (!instream.eof())
{
getline(instream, expression);
cout << expression + " ";
in.setInfix(expression);
in.getInfix();
cout << in.getInfix();
try{
cout << in.eval();}

catch(const DivByZero& e) {

cout << e.what();}
}

instream.close();
}

else
{

cout << "Enter an expression to evaluate " << endl;
cin >> expression;
in.setInfix(expression);
cout << in.getInfix() << "\n";
try{
cout << in.eval() << endl;}

catch(const DivByZero& e) {

cout << e.what();}
}

cout << "Would you like to run again? ";
cin >> input;

}while( input == 'y' && !instream.eof() );
return 0;
}

Last edited on
Can you post your Infix.h as well.
Some tips:
Always check if the stack is not empty before using pop .
Check your parameters before using them like so.
1
2
3
4
5
void Infix::execute(LinkedStack<int>& valueStack, LinkedStack<char>& operatorStack)
{
   assert(!valueStack.isEmpty());
   assert(!operatorStack.isEmpty());
}

Segmentation fault are the worst, I found a great helper for those. Checkmarx helps me detect those kind of vulnerabilities among the code and I fix them long before they happen.
I think it's important to handle those as soon as possible because when i get them it's getting hard to find the place to fix it.
Good luck!
Ben.
Topic archived. No new replies allowed.