Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Compiler not working as expected? (if- evaluation order)

29 views
Skip to first unread message

JiiPee

unread,
Oct 13, 2017, 9:27:18 AM10/13/17
to
This is a bit difficult to find, so asking...


I thought that when using if-statement the order of logical statements
are evaluated from left to right?!

But using my VS 2017 I am suprised that the following crashes:

        vector<int> vec;

        int a = 5;

        if ((0 < vec.size() - 1) && (a > vec[8]))
            ++a;

I thought the: (0 < vec.size() - 1) is evaluated first, and if its false
then the second is not evaluated. But it crashes here because of vec[8].

Or is there something else in my code possible crashing it? This should
not crash? But the crash report is about the index 8.....


(here is my real code if interested:

        if ((playerInd < playersPrintIndex.size() - 1) &&
            (order + 1 > playersPrintIndex[playerInd] + compactOffset))
            ++playerInd;

playerInd= 0 and playersPrintIndex.size = 0


)

Öö Tiib

unread,
Oct 13, 2017, 9:36:59 AM10/13/17
to
On Friday, 13 October 2017 16:27:18 UTC+3, JiiPee wrote:
> This is a bit difficult to find, so asking...
>
>
> I thought that when using if-statement the order of logical statements
> are evaluated from left to right?!
>
> But using my VS 2017 I am suprised that the following crashes:
>
>         vector<int> vec;
>
>         int a = 5;
>
>         if ((0 < vec.size() - 1) && (a > vec[8]))
>             ++a;
>
> I thought the: (0 < vec.size() - 1) is evaluated first, and if its false
> then the second is not evaluated. But it crashes here because of vec[8].

It is, but trouble is that vec.size() is unsigned number 0 and subtracting
1 from it results with very large unsigned number that is larger than 0
and so first operand of && evaluates to true. Therefore second operand
is also evaluated.

guinne...@gmail.com

unread,
Oct 13, 2017, 9:45:04 AM10/13/17
to
On Friday, 13 October 2017 14:27:18 UTC+1, JiiPee wrote:

> I thought that when using if-statement the order of logical statements
> are evaluated from left to right?!

They are.

> But using my VS 2017 I am suprised that the following crashes:

I'm not.

>         vector<int> vec;
>
>         int a = 5;
>
>         if ((0 < vec.size() - 1) && (a > vec[8]))
>             ++a;

Your (0 < vec.size() - 1) is the same as (vec.size() > 1): it tests whether
vec contains at least two items (vec[0] and vec[1]). It gives no guarantees
about the existence of the 9th element (vec[8]).

> I thought the: (0 < vec.size() - 1) is evaluated first,

It is.

< and if its false
> then the second is not evaluated.

Correct.

> But it crashes here because of vec[8].

Of course. You only know that there are at least two elements present,
yet you're trying to access the 9th. It probably doesn't exist.

> Or is there something else in my code possible crashing it?

Your logic.

> This should
> not crash?

It could if you don't have at least 9 elements.

> But the crash report is about the index 8.....
>
>
> (here is my real code if interested:

I'm not.
>snip<

JiiPee

unread,
Oct 13, 2017, 9:58:23 AM10/13/17
to
wow, you are right!! lol, I am blind. I do understand this... so just a
newbie error there. Thanks.

JiiPee

unread,
Oct 13, 2017, 10:00:22 AM10/13/17
to
On 13/10/2017 14:44, guinne...@gmail.com wrote:
> On Friday, 13 October 2017 14:27:18 UTC+1, JiiPee wrote:
>
>> I thought that when using if-statement the order of logical statements
>> are evaluated from left to right?!
> They are.
>
>> But using my VS 2017 I am suprised that the following crashes:
> I'm not.
>
>>         vector<int> vec;
>>
>>         int a = 5;
>>
>>         if ((0 < vec.size() - 1) && (a > vec[8]))
>>             ++a;
> Your (0 < vec.size() - 1) is the same as (vec.size() > 1): it tests whether
> vec contains at least two items (vec[0] and vec[1]). It gives no guarantees
> about the existence of the 9th element (vec[8]).
>
>> I thought the: (0 < vec.size() - 1) is evaluated first,
> It is.
>

ok but if done first and false it would not evaluate the second, like
Tiib says.

Vlad from Moscow

unread,
Oct 13, 2017, 1:36:16 PM10/13/17
to
This statement

if ((0 < vec.size() - 1) && (a > vec[8]))
             ++a;

is logically wrong. The first subexpression of the condition says nothing about whether there a 9-th element in the vector. Moreover if the vector is empty then this subexpression will yield true due to the fact that the type of the returned value of the member function size() is unsigned.

So it would be correctly to write at least like

if ( not (vec.size() < 9 ) and (a > vec[8]))
             ++a;



пятница, 13 октября 2017 г., 16:27:18 UTC+3 пользователь JiiPee написал:

JiiPee

unread,
Oct 14, 2017, 9:22:27 AM10/14/17
to

vec contains many elements. This is not my real code, I only did it to
illustrate the problem. But I know already it was the unsigned issue. thanks
0 new messages