Comparing a negative value against std::vector.size()
#1 Crossbones+ - Reputation: 924
Posted 15 December 2012 - 04:23 PM
I am having a minor issue in my code, I have replaced an Integer variable with the std::vector.size() in a comparison statement (please see below):
[source lang="cpp"]if(selectedOption_ > (optionsList_.size() - 1)) { selectedOption_ = (optionsList_.size() - 1); } else if(selectedOption_ < firstSelectableOption_) { // Highlighted option does not trigger as going less than 0. selectedOption_ = firstSelectableOption_; }[/source]
Before, I compared SelectedOption to an Integer (numOptions) and this worked as I expected, if it went greater than said number then it would be reset to numOptions, else if it when less than 0(firstSelectableOption), it would be set to zero.
Once I replaced it with size(), if the SelectedOption goes less than zero, it triggers the 'greater-than' clause and sets the SelectedOption equal to whatever the size()-1 is. I have looked through the debugger and the numbers are coming back as expected: SelectedOption becomes -1, and size in this instance being 2. So what is going on here. The quick-fix solution I am using is this:
[source lang="cpp"]if(selectedOption_ < firstSelectableOption_) { // Highlighted option does not trigger as going less than 0. selectedOption_ = firstSelectableOption_; } else if(selectedOption_ > (optionsList_.size() -1)) { selectedOption_ = (optionsList_.size() - 1); }[/source]
This ensures that the check for going less than zero occurs first, which solves the issue I am having, but does not answer the question it puts forward.
In short, is there something I am missing that would be firing the if-else in reverse order. Also, I have checked and am not setting size equal to something different elsewhere.
Thanks in advance for your time,
Regards,
Stitchs.
#2 Moderators - Reputation: 5043
Posted 15 December 2012 - 04:29 PM
Edited by JTippetts, 15 December 2012 - 04:32 PM.
#3 Crossbones+ - Reputation: 924
Posted 15 December 2012 - 04:37 PM
Regards,
Stitchs.
#4 Moderators - Reputation: 5043
Posted 15 December 2012 - 04:52 PM
When would my negative Integer get cast to an unsigned int? Does this happen in the comparison at all?
Regards,
Stitchs.
Yes.
Your compiler should be warning you about the signed to unsigned comparison, as well. It's a good idea to pay attention to those kinds of warnings, as subtle bugs like this can result.
#5 Marketplace Seller - Reputation: 8957
Posted 15 December 2012 - 05:16 PM
if(myInt < (int)vector.size())
Side-effects: if your size_t ever reaches higher than 31^2 (about two billion), your value will likely loop around to -31^2 (around negative two billion).
if(selectedOption_ >= int(optionsList_.size()))
{
}
else if(selectedOption_ < firstSelectableOption_)
{
}
Edited by Servant of the Lord, 15 December 2012 - 05:20 PM.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal
#8 Crossbones+ - Reputation: 924
Posted 15 December 2012 - 05:33 PM
Regards,
Stitchs.
#9 Members - Reputation: 1842
Posted 15 December 2012 - 05:36 PM
#10 Crossbones+ - Reputation: 1177
Posted 15 December 2012 - 06:28 PM
In that case I would suggest you also set the flag treat warnings as errors, which forces you to fix them.Also, whack up your warning level to the highest possible (level 4 I think), and fix all of them. If you get warnings from 3rd party headers, you can use #pragma warning(push:####) and #pragma warning(pop) to enable/disable them around specific headers (where #### is the warning you want to disable).
#11 Members - Reputation: 531
Posted 15 December 2012 - 06:47 PM
Visual C++ could show such warning, but the compiler must have set the correct warning level.
Why such strange comparision? Maybe you would construct a bit more complex if.
Such magic values, etc sometimes are unsafe and strange for reading.






