AMS
AMS

Reputation: 73

Yet another question related to sequence points

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.

Upvotes: 7

Views: 267

Answers (6)

Inverse
Inverse

Reputation: 4476

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.

Upvotes: 2

Patrick
Patrick

Reputation: 8318

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.

Upvotes: 1

Binary Worrier
Binary Worrier

Reputation: 51739

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.

Upvotes: 11

please delete me
please delete me

Reputation: 731

++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.

Upvotes: 0

sharptooth
sharptooth

Reputation: 170569

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.

Upvotes: 9

icecrime
icecrime

Reputation: 76835

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.

Upvotes: 0

Related Questions