jasonkim
jasonkim

Reputation: 706

weird segmentation fault

What I have been doing is a reverse polish notation calculator.I have met this error when compiling my program below:

class Expression {
       protected:
                 string exp;
                 int value;
       public:
                 void getExp();//extract the exp from Expression
                 void setExp(string s);//store s in Expression
                 void setValue(int n);//store n in Expression
                 int evaluate();//extract the value from Expression
 };


 ...
 class binary : public Expression {
  public:
          void binaryyy(Expression *x1,Expression *x2,string op){
             if(op=="+"){
                 setValue(x1->evaluate()+x2->evaluate());
                 string x;
                 x.append(x2->getExp());
                 x.append("+");
                 x.append(x1->getExp());
                 setExp(x);
                }
             else if(op=="-"){
                 setValue(x1->evaluate()-x2->evaluate());
                 string x;
                 x.append(x2->getExp());
                 x.append("-");
                 x.append(x1->getExp());
                 setExp(x);
                }
             }
 };

then in my main function:

 int main(){
  ...
  Expression *stack[10];
  int p=9,i;//p refers to one slot above the top element of the stack
  for(i=0;i<10;i++) stack[i]=NULL;
  ...
  string s_input;
  getline(cin,s_input);
  istringstream sss(s_input);

  while(!sss.eof() && p>-2){
  sss>>s;
  if(s=="+" || s=="-")
     binary *b = new binary;
     b->binary(stack[p+1],stack[p+2],s);
     stack[p+1]=NULL;
     stack[p+2]=b;
     p++;
  }
  else if(s.isNumber())//s.isNumber() might not exist.it means that s is number...
  {
     Expression *c=new Expression;
     istringstream ss(s);
     int temp;
     ss>>temp;
     c->setValue(temp);
     stack[p]=c;
     p--;
  }
  }
  ...

I have checked very carefully for any possible illegal allocation or call of memory slots and all.NO CLUE...

PLUS:p would not overrun in this case.

Upvotes: 0

Views: 125

Answers (4)

Ed Swangren
Ed Swangren

Reputation: 124622

int p=9,i;//p refers to the top of the stack

...

b->binary(stack[p+1],stack[p+2],s);
stack[p+1]=NULL;
stack[p+2]=b;

Well there is an overrun. stack[p+1] is stack[10] and stack[p+2] is stack[11] in your ten element array. You're writing past the bounds of your array (unless the ... contains code which adjusts p correctly, but I have no way to know).

After that problem is fixed you need to initialize your stack array. Currently you have an array of 10 pointer to Expression. None of them are initialized to point to anything valid though, and you later dereference them.

Also...

b->binary(...)

Won't compile.

Upvotes: 2

Adam Trhon
Adam Trhon

Reputation: 2935

If you are on linux, take a look at my answer here. It is about valgrind, a tool that can help you debug runtime problems.

Upvotes: 0

yati sagade
yati sagade

Reputation: 1375

stack can hold 10 pointers to Expressions, i.e., from index 0 to 9. The stack pointer p starts from 9, but in your code, you say stack[p+1] and stack[p+2], both of which are examples of illegal indexing(They respectively translate to stack[10] and stack[11], which are nonexistent).

Upvotes: 0

First, compile with all warnings enabled and with debugging information produced. On Linux, that means g++ -Wall -g.

Then, learn to use the debugger (on Linux, gdb or a graphical front-end like ddd).

Upvotes: 1

Related Questions