-
April 27th, 2011, 05:51 PM
#1
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.
-
April 27th, 2011, 05:56 PM
#2
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()).
-
April 27th, 2011, 05:59 PM
#3
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.
-
April 27th, 2011, 06:25 PM
#4
Re: Boolean expression evaluating to false, should be true.
Originally Posted by Alterah
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
-
April 27th, 2011, 07:43 PM
#5
Re: Boolean expression evaluating to false, should be true.
Originally Posted by Paul McKenzie
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.
-
April 27th, 2011, 08:12 PM
#6
Re: Boolean expression evaluating to false, should be true.
Originally Posted by Alterah
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.
-
April 28th, 2011, 05:50 AM
#7
Re: Boolean expression evaluating to false, should be true.
Originally Posted by GCDEF
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.
-
April 28th, 2011, 07:17 AM
#8
Re: Boolean expression evaluating to false, should be true.
Originally Posted by Lindley
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.
-
April 28th, 2011, 10:10 AM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|