When is postincrement i ++ executed?

Possible duplicate:
Undefined Behavior and Sequence Points

In C ++, at the machine code level, when is the postincrement ++ statement executed?

The table indicates that postfix ++ statements are level 2: this means that in

int x = 0 ; int y = x++ + x++ ; // ans: y=0 

First, postfix c ++ is executed.

However, it would seem that the logical operation of this line is that the addition occurs first (0 + 0), but how does this happen?

I suppose this is the following:

 // Option 1: // Perform x++ 2 times. // Each time you do x++, you change the value of x.. // but you "return" the old value of x there? int y = 0 + x++ ; // x becomes 1, 0 is "returned" from x++ // do it for the second one.. int y = 0 + 0 ; // x becomes 2, 0 is "returned" from x++... but how? // if this is really what happens, the x was already 1 right now. 

So another option, although x ++ is higher in the priority table, x + x, the code generated due to x ++ is inserted below the add operation

 // Option 2: turn this into int y = x + x ; // x++ ; x++ ; 

This second option seems to make more sense, but I'm interested in the order of operations here. In particular, when does x change?

+2
c ++ post-increment
source share
4 answers

Instead of retelling the details of the UB example, I will talk about the following example, which works great:

 int a = 0, b = 0; int c = a++ + b++; 

Now, operator precedence means the last line is equivalent:

 int c = (a++) + (b++); 

And not:

 int c = (a++ + b)++; // compile time error, post increment an rvalue 

On the other hand, the semantics of incremental incrementation is equivalent to two separate instructions (hence only the mental picture):

 a++; // similar to: (__tmp = a, ++a, __tmp) // -- ignoring the added sequence points of , here 

That is, the original expression will be interpreted by the compiler as:

 auto __tmp1 = a; // 1 auto __tmp2 = b; // 2 ++a; // 3 ++b; // 4 int c = __tmp1 + __tmp2; // 5 

But the compiler is allowed to change the order of 5 instructions until the following restrictions are satisfied (where x>y means x must be executed before y or x preceded by y ):

 1 > 3 // cannot increment a before getting the old value 2 > 4 // cannot increment b before getting the old value 1 > 5, 2 > 5 // the sum cannot happen before both temporaries are created 

There are no other restrictions in the execution order of the various instructions, so the following valid sequences are:

 1, 2, 3, 4, 5 1, 2, 5, 3, 4 1, 3, 2, 4, 5 ... 
+6
source share

it

 int y = x++ + x++ ; 

is undefined behavior. Anything can happen, including some unreasonable results, a program crash, or something else. Just don't do it.

+6
source share

In C ++, there are things called sequence points . "If you change a value more than once without an intermediate point in the sequence, the behavior is undefined .

Consider the following:

 int x = 0; int y = x++ + x++; 

The y value may be 0, 1, or some other completely random value.

Bottom line, do not do this. Nothing good will come of it. :-)

+3
source share

In your case, it looks like this:

When you use x++ , x increases after the operation completes.

 int y = x++ + x++ ; // First add 0+0 // Increment x // Increment 

While

 int y = ++x + ++x ; // Add (increment x) and (increment x) = 1+1 = 2 

However, different compilers will handle it differently, and your application may crash if you double it in the same expression.

+1
source share

All Articles