0

Will this code lead into undefined behavior in case the array is just filled with 0's? Or could a compiler do some checks for less critical conditions i.e. "checking automatic non pointer values as first"

uiaDigit[10];

for (sizeIndexI = 0; sizeIndexI < 10 && uiaDigit[sizeIndexI] == 0; sizeIndexI++)
    ;

I would guess something like a reading direction as if it is

"An AND comparison will always be read from left to the right and break out at first false"

Is at most implementation defined behavior, isn't it?

So will a compiler get it that he has to break out anyway if sizeIndexI gets 10, or will he compare 'uiaDigit[10] == 0' anyway?

dhein
  • 6,431
  • 4
  • 42
  • 74
  • thanks for all the fast answers, if anyone could quote me it from standard with reference I would be realy thankfull. – dhein Sep 16 '13 at 14:42
  • It's easy to find the evaluation order in the standard. We could look it up, or you could. – David Heffernan Sep 16 '13 at 14:44
  • true story, i didn't knew that `&&` is also a sequence point sign. But wouldn't it be better for maybe later watches to see an asnwer from the standard, if soem one isn't that comform with it? – dhein Sep 16 '13 at 14:45
  • 1
    @Zaibis You are right. An answer is better if it quotes the standard. – David Heffernan Sep 16 '13 at 14:48

4 Answers4

1

You are correct, the && is a sequence point. See the answer for this question: Short circuit evaluation and side effects

Community
  • 1
  • 1
Neil
  • 11,059
  • 3
  • 31
  • 56
  • assuming uiaDigit is initialised to be full of 0. – Neil Sep 16 '13 at 14:41
  • hang on a minute, you've just reversed the comparison in the question. Is it sizeIndexI < 10 && uiaDigit[sizeIndexI] == 0 or uiaDigit[sizeIndexI] == 0 && sizeIndexI < 10 ? – Neil Sep 16 '13 at 14:42
1

For the && operator the evaluation is done from left to right. So if you want to have the pointer operation be skipped put it to the most right part of the expression.

C standard "6.5.13 Logical AND operator"

4 Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated.

alk
  • 69,737
  • 10
  • 105
  • 255
1

Logical and(&&) will be evaluated from left to right and will short circuit if the first operand evaluates to 0. The C99 draft standard in section 6.5.13 Logical AND operator paragraph 4 says(emphasis mine):

Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; there is a sequence point after the evaluation of the first operand. If the first operand compares equal to 0, the second operand is not evaluated.

So this code:

sizeIndexI < 10 && uiaDigit[sizeIndexI] == 0

will not evaluate uiaDigit[sizeIndexI] == 0 when sizeIndexI < 10 evaluates to 0 which will prevent the code from accessing outside the bounds of uiaDigit.

If you do not initialize uiaDigit and it is a local variable it will contain contain indeterminate values and the program will not have determinable behavior.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

Your expression is evaluated left to right with short-circuit evaluation. The compiler does not change the evaluation order because it feels that one condition if more likely to lead to UB than another. That's just wishful thinking on your part!

From the standard, ISO/IEC 9899:201x N1570 draft:

6.5.13 Logical AND operator

....

Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares equal to 0, the second operand is not evaluated.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I am not so sure about a compiler not determining evaluation order based on the presence, or even possible presence, of undefined behavior. Unfortunately, it could favor selecting for undefined behavior, rather than against it, because this is a boon to the optimizer. Imagine you are the optimizer, tasked with the job of optimizing performance at the expense of everything else except the semantic rules. If you find undefined behavior, you can say “Yippee, none of the other stuff matters, I can eliminate it!” – Eric Postpischil Sep 16 '13 at 16:06
  • @EricPostpischil Hmm. Devious. It's still not going to evaluate right to left. But your scenario is valid if the UB is in the left-most term. – David Heffernan Sep 16 '13 at 16:26