|
-
May 20th, 2006, 06:10 PM
#1
Thread Starter
Frenzied Member
stack problem
Take the following code and look closely at the bold line:
Code:
if (function_stack.size() > 0)
{
string::size_type op_prec_2 = function_precedence[ function_operators.find(function_stack.top()) ];
string::size_type op_prec_1 = function_precedence[ function_operators.find(eq[counter]) ];
if (op_prec_2 <= op_prec_1)
{
ostringstream oss;
oss << function_stack.top();
output_queue.push( oss.str() );
if (function_stack.size() > 0)
{
function_stack.pop();
}
}
}
It simply pops something off the top of the stack, but it causes a crash. I've tried using a try-catch clause and it still gives me the same thing without an error message. I don't see why this would cause a problem, since I'm checking the size first.
Also, if I change this line:
if (op_prec_2 <= op_prec_1)
to this:
if (op_prec_2 >= op_prec_1)
it works fine, but for some reason the top line causes the stack not to be able to pop();
Does anyone have a clue why this is happening?
PS: I've done cout statements all through out the program. Everything is good.
-
May 20th, 2006, 08:28 PM
#2
Re: stack problem
The size check is actually redundant. Have you stepped through the code and in particular into the pop() with a debugger?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 20th, 2006, 09:41 PM
#3
Thread Starter
Frenzied Member
Re: stack problem
 Originally Posted by CornedBee
The size check is actually redundant. Have you stepped through the code and in particular into the pop() with a debugger?
That's what I thought. When I ran the debugger, it gave a segmentation fault error with the second size check I posted.... But not with the first. I don't see how calling pop() could illegally access memory... Any ideas on that?
-
May 21st, 2006, 06:02 AM
#4
Re: stack problem
No. That's why I suggested you step INTO the pop() call.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 21st, 2006, 06:39 AM
#5
Thread Starter
Frenzied Member
Re: stack problem
CB, I did all that yesterday. This morning, (after my computer restarted), everything is working fine.. I'm guessing the numeric comparision for the second statement caused a different number of pop() statements and some of which were illegal. The good thing is it's working. The bad thing is I can't step into the pop() statement like you told me to.
Thanks for the help. Hopefully the problem won't come back.
-
May 21st, 2006, 07:53 PM
#6
Thread Starter
Frenzied Member
Re: stack problem
Ahh, it's doing it again. I don't get it. If stack is called twice, then it crashes, but if I turn the compare statement around, it runs fine with four calls.
Is there an alternative to the stack class that provides easy operations and LIFO?
What do you suggest I do CB?
-
May 22nd, 2006, 05:48 AM
#7
Re: stack problem
Create a minimal testcase that reproduces the error and runs on Linux, zip it up and send it to me. The error intrigues me.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 22nd, 2006, 02:53 PM
#8
Thread Starter
Frenzied Member
Re: stack problem
Thanks.
I'll attach it here. I really couldn't slim the code down any, but it's really only one method in a class, so it's not a big deal.
This isn't homework, so take your time and only look at it when you feel like
Thanks for any help you give. This problem has really annoying me and the only thing holding me back.
-
May 22nd, 2006, 03:37 PM
#9
Thread Starter
Frenzied Member
Re: stack problem
I guess it would help if i attached it.
-
May 31st, 2006, 02:41 PM
#10
Thread Starter
Frenzied Member
Re: stack problem
Well, CB, I figured out the problem. First off, the code was fairly messy so I cleaned it up. Then I found out I was working with the operator precedence all wrong.
-
May 31st, 2006, 03:14 PM
#11
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|