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<