11

Possible Duplicate:
Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…)
increment values in printf

I have a two double arrays x and y and integer i. My question is whether the statement:

double res = x[i] * y[i++];

is always equal to the statement:

double res = x[i] * y[i];
i++;

Is it possible that some compilers would change x[i] * y[i++] into y[i++] * x[i], which obviously produces different result?

Community
  • 1
  • 1
Serg
  • 13,470
  • 8
  • 36
  • 47
  • Why does it matter? If you want a specific order, just write it in that order. – Bo Persson Dec 05 '12 at 18:34
  • @BoPersson. I thought to write explanation why I need it, but didn't want to spoil the question. Shortly, I need to accelerate `double vectors_dot_product(double *x, double *y, int n);` function, and found that `x[i] * y[i++] + x[i] * y[i++] + ...;` is faster than `x[i] * y[i] + x[i+1] * y[i+1] + ...;` – Serg Dec 05 '12 at 18:38

4 Answers4

13

No -- x[i] + y[i++] has undefined behavior. You're modifying the value of i and also using the value i without an intervening sequence point, which gives undefined behavior1.


  1. In C++11 the standard has eliminated the "sequence point" terminology, but the effect remains the same -- the two are unordered with respect to each other.
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 1
    +1 first answer with correct language. – slebetman Dec 05 '12 at 18:19
  • 3
    This has nothing to do with the commutativity of the multiplication operator. If the operation was `x[i] - y[i++]` (which can't be reversed), the behavior of `i` in this situation would still be undefined. – Ken Bloom Dec 05 '12 at 18:25
8

No, it is undefined when the increment occurs.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
6

The code modifies i and uses its value without an intervening sequence point, so the behavior is undefined. The language definition does not impose any requirements here.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

No,

value of i++ + i++ are undefined in C and C++.

if you read a variable twice in an expression where you also write it, the result is undefined. Don't do that. Another example is:

v[i] = i++;

Undefined means its COMPILER DEPENDENT.
Some compiler could warn you also as undefined because the order of evaluation. A very good reference for C++

1 http://www.stroustrup.com/bs_faq2.html#evaluation-order

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Still needs a bit of work -- `v[i] = i++;` is undefined even though it may not involve any function or passing arguments at all. – Jerry Coffin Dec 05 '12 at 18:30
  • @Serg: Welcome serg ! I glad if I can help :) – Grijesh Chauhan Dec 05 '12 at 18:33
  • @Jerry Coffin: Thanks for informing...It mention in the link I provided. – Grijesh Chauhan Dec 05 '12 at 18:36
  • @GrijeshChauhan - sorry to pile on, but this is **not** about order of evaluation. The order of evaluation of function arguments is **unspecified**, which means that it is compiler dependent: the compiler can arbitrarily choose an order. Here there are no function arguments to be evaluated. The behavior is **undefined**, which means all bets are off. In a sense it's compiler dependent, since it obviously depends on the code the compiler generates, but the code that the compiler generates (if it does...) doesn't have to do anything sensible. – Pete Becker Dec 05 '12 at 19:08