CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2009
    Posts
    440

    Boolean expression evaluating to false, should be true.

    Hello,

    I am running into a very peculiar problem. Here's a code snippet:

    Code:
    for( int i = 0; ( ( mStageInstrCounter[WB_STAGE] <= mCodeList.size() ) || ( i < mCodeList.size() ) ); i++ )
    {
    //code here
    }
    mStageInstrCounter is a vector of 8 ints.
    WB_STAGE is a constant equal to 7.
    mCodeList.size() is equal to 3 (in my sample case).

    Now, this part:

    Code:
    (mStageInstrCounter[WB_STAGE] <= mCodeList.size())
    Keeps evaluating to false. I have run this thing through my debugger and mCodeList.size() is indeed 3. In addition mStageInstrCounter[WB_STAGE] is equal to -1 (it is what each of the values in the vector get initialized to. I am trying to figure out why this is the case.

    It functions as expected if I create two new ints. Assign one the value of mStageInstrCounter[WB_STAGE] and another to hold mCodeList.size(). This seems like a hackish way of doing things here. Thanks for any explanation.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Boolean expression evaluating to false, should be true.

    Your problem is that mCodeList.size() is an unsigned value. When an unsigned value is compared to a signed value, the comparison is done as if both values are unsigned. (Your compiler should issue a warning about signed/unsigned mismatches. Do not ignore compiler warnings.)

    Basically, this means that -1 is being interpreted as 2^32-1, which is the unsigned interpretation of the same bit pattern (assuming a 32-bit integer size).

    One solution would be to compare to static_cast<int>(mCodeList.size()).

  3. #3
    Join Date
    Aug 2009
    Posts
    440

    Re: Boolean expression evaluating to false, should be true.

    Oh. Well, that makes sense. It was definitely peculiar going through this with the debugger and not realizing that. I think I am just going to define a new variable called mCodeSize and set it equal to mCodeList.size(). Will solve this problem. Thanks for the info.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Boolean expression evaluating to false, should be true.

    Quote Originally Posted by Alterah View Post
    Oh. Well, that makes sense. It was definitely peculiar going through this with the debugger and not realizing that. I think I am just going to define a new variable called mCodeSize and set it equal to mCodeList.size(). Will solve this problem. Thanks for the info.
    Didn't your compiler give a warning that you are using signed/unsigned variables in a potentially unsafe manner?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2009
    Posts
    440

    Re: Boolean expression evaluating to false, should be true.

    Quote Originally Posted by Paul McKenzie View Post
    Didn't your compiler give a warning that you are using signed/unsigned variables in a potentially unsafe manner?

    Regards,

    Paul McKenzie
    After looking at the output it did. I was working on my laptop and in order to maximize my screen space I closed out the log section of my IDE. It's been a long day.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Boolean expression evaluating to false, should be true.

    Quote Originally Posted by Alterah View Post

    Code:
    for( int i = 0; ( ( mStageInstrCounter[WB_STAGE] <= mCodeList.size() ) || ( i < mCodeList.size() ) ); i++ )
    {
    //code here
    }
    What's with the superfluous parens?

    This is all you need.
    Code:
    for( int i = 0;  mStageInstrCounter[WB_STAGE] <= mCodeList.size()  ||  i < mCodeList.size(); i++ )
    To me, it's easier to read when you don't add stuff that doesn't need to be there.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Boolean expression evaluating to false, should be true.

    Quote Originally Posted by GCDEF View Post
    To me, it's easier to read when you don't add stuff that doesn't need to be there.
    Only to a certain extent. If you're relying on a complete knowledge of the order of operations beyond what the average coder might know by heart (which takes precedence----right-shift or bitwise and?), a pair of technically superfluous parens might still be helpful.

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Boolean expression evaluating to false, should be true.

    Quote Originally Posted by Lindley View Post
    Only to a certain extent. If you're relying on a complete knowledge of the order of operations beyond what the average coder might know by heart (which takes precedence----right-shift or bitwise and?), a pair of technically superfluous parens might still be helpful.
    That's not the case here though. When I was looking at the code it just added clutter and confusion.

  9. #9
    Join Date
    Aug 2009
    Posts
    440

    Re: Boolean expression evaluating to false, should be true.

    I've actually simplified it further:

    Code:
    for( int i = 0;  mStageInstrCounter[WB_STAGE] < mCodeSize; i++ )
    It is ok for i to become larger than the code size (this is a Mips simulator). And, the <= was correct at one point, but after changes my vectors to be consistent, the < is correct now.

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

Featured