Results 1 to 11 of 11

Thread: stack problem

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: stack problem

    Quote 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?

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  5. #5

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  6. #6

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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?

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  8. #8

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  9. #9

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: stack problem

    I guess it would help if i attached it.

  10. #10

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    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.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: stack problem

    Great!
    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
  •  



Click Here to Expand Forum to Full Width