last day (14 days later) » 

12:34 AM
-2
Q: Having trouble with segmentation fault in program

Hayden HolliganI'm making a RPN(reverse polish notation calculator). So if I enter 3 4 + My output will be (3 + 4) (prettyprint) = 7 (evaluate) This is the test I'm trying and I'm getting a Seg fault in vim (with g++). In Xcode it doesn't seem to be adding the Binary expression correctly, although it could be ...

 
Why not use std::stack?
 
@MrEricSir I honestly don't know how I can. If I give the code that I THINK isn't working, I could be giving code that's fine and something else is wrong.
@PaulMcKenzie can't, school project and they won't allow it
@pm100 in the line cout << "= " << expstack[0]->evaluate() << endl; expstack[0] is empty. i don't know why it's empty since binary SHOULD have been pushed on last.
 
@HaydenHolligan - One reason segmentation faults occur is due to a mismanagement of pointers and/or dynamically allocated memory. You are allocating in one function, deallocating in another function, and I wouldn't be surprised if somewhere things get fouled up.
 
@MrEricSir I've been looking through my code trying to figure out what's wrong for the past 4 hours, if I knew where the problem was I could probably identify what the problem was and how to fix it
 
@HaydenHolligan Is your Binary and Unary classes derived from a base class? If so, does your base class have a virtual destructor? If it does not have a virtual destructor, your code exhibits undefined behavior.
 
12:34 AM
@PaulMcKenzie yes it's derived from expression.h which does have a virtual destructor.
 
@HaydenHolligan - It seems by the layout of your code, it is easy to lose track of who owns the pointers that are allocated. For example, all of a sudden, Binary deletes arg1 and arg2. Where did these pointers come from? And you do really need to use the debugger -- there is no way you can write code like this and hope that you never need to debug it using a debugger.
 
@PaulMcKenzie arg1 and arg2 would be 3 and 4 in the example. If a binary op is encountered, it can be assumed there were 2 expressions before the operator. I haven't really learned how to use a debugger yet - they don't think it's important to teach us that in class. I'm slowly learning on my own in Xcode but it's tricky
 
@HaydenHolligan - What if there is no binary op? You go and delete arg1 and arg2 anyway. If those pointers aren't 0, you're issuing a delete call on an invalid pointer value. But again, the issue is that no one entity owns the pointers. You delete some here, allocate some there, assign it to an entity somewhere else etc. That is the quick way to a segmentation fault. So use your debugger, step through your code, and identify which line produces the segmentation fault.
 
@PaulMcKenzie arg1 and arg2 are only popped if it is a binary op( hence the if statement). is there a place to learn how to debug using Xcode? most of the results on google seem pretty dated
 
@HaydenHolligan - If you can't use the debugger, then place output statements to get an idea of what part of the code executed before the segmentation fault occurred. Also, we don't have all of your code -- it is near impossible to tell you what the issue is without us running the code ourselves and debugging the code.
@HaydenHolligan - BTW, this is a case where std::shared_ptr<Expression> would solve the issue with the pointer ownership, where you push() those instead of Expression *. All the code basically stays the same, without the delete and the pointers delete themselves when the last object using it dies.
Why is your Binary constructor this:

Binary::Binary(const char& symbol, Expression * const& arg1, Expression * const& arg2) {}

instead of this:
Binary::Binary(const char& symbol, Expression *arg1, Expression *arg2) {}
?
 
 
2 hours later…
2:28 AM
hi! i found the issue. You were actually at the right spot with the constructor. The instructors want us to do that for whatever reason. I forgot the member initialization list..
Binary::Binary(const char& symbol, Expression * const& arg1, Expression * const& arg2) : symbol(symbol), arg1(arg1), arg2(arg2) {}
it works fine now. i spent 7 hours trying to fix that :(
 

last day (14 days later) »