7

Yes i read the article on sequence points. However i could not understand why ++i = 2 would invoke undefined behavior? The final value of i would be 2 regardless of anything, so how come the expression is ub?

code snippet

int main()
{
  int i =0;
  ++i=2;
  return 0;
}

Sorry my english is not very good.

Community
  • 1
  • 1
AMS
  • 73
  • 3

6 Answers6

11

It looks obvious to you, because obviously i will first be assigned i+1, then second be assigned the value 2.

However, both of these assignments happen within the same sequence point, therefore it's up to the compiler to which happens frist and which happens second, therefore different compiler implementations can generate code that will give different results, therefore it's UB.

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
  • 4
    Yeap, and also the compiler is not required to produce the same program in different compiles. Prints 0 on ane compile, erases disk on another. – sharptooth Nov 22 '10 at 14:21
9

You observe that value will be what you claim, that's how UB can manifest itself among other possible scenarios. The program might output what you expect, output some unrelated data, crash, corrupt data or spend all your money ordering pizza. Once C++ standard says that some construct is UB you should not expect any specific behavior. Observed results can vary from one program run to another.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • but how could the result be different from 2? i tried on a few online and offline compilers including gcc, msvc++, intel c++. i got nothing different from 2. – AMS Nov 22 '10 at 14:17
  • 1
    @AMS: What if the program also spent all your money or sent all your passwords to a third party (http://stackoverflow.com/questions/908872/whats-the-worst-example-of-undefined-behaviour-actually-possible/3554343#3554343)? – sharptooth Nov 22 '10 at 14:20
  • And that's not a joke at all - I encourage you to actually follow the link and read the answer behind it. – sharptooth Nov 22 '10 at 14:22
2

The undefined behavior occurs because a compiler could implement the following code:

++i = 2;

as either:

i = 2;
++i;

or

++i;
i = 2;

It's unspecified in the language, a compiler could choose to implement either of the above. The first would produce 3 and the second 2. So it's undefined.

Inverse
  • 4,408
  • 2
  • 26
  • 35
1

Calling ++i = 2; does not in and of itself invoke undefined behaviour; any compiler can, if it wants, do a very defined action upon reaching that code. However the c++ standard states that such an operation is undefined,therefore a compiler may do something unexpected (like delete all the files on the C drive or send a text message to the pope) and still be a compliant compiler. The only thing that makes this UB is that the standard says it is UB.

Perhaps the most important point is that one version of a compiler may do something different from the next version of the same compiler.

Patrick
  • 8,175
  • 7
  • 56
  • 72
0

From the exact same link you are providing :

  • Furthermore, the prior value shall be accessed only to determine the value to be stored.

What does it mean? It means if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written.

Here on the left hand side of operator =, the access to i is not involved in the computation of the value written.

icecrime
  • 74,451
  • 13
  • 99
  • 111
0

++i (should be) an rvalue, and hence, can't be used as a lvalue, but (++i) = 2; should work fine. I don't believe this is UB, but, as always, I might be wrong.

please delete me
  • 711
  • 2
  • 9
  • 17
  • Yes you are wrong. Adding parentheses does not change the parsing of that expression, prefix operator++ returns an lvalue. The UB is a result of writing to the same lvalue twice in the same sequence point. – Greg Rogers Nov 24 '10 at 18:52