zaterdag 16 juni 2007

sequence point

A sequence point is a point in program execution at which all side effects are guaranteed to be evaluated before going to the next step.
In C++ the semicolon in a statement marks a sequence point.
That means that all changes made by assignment operators, increment operators, and decrement operators in a statement must take place before a program proceeds to the next statement.
Some operators have sequence points.
Also the end of any full expression is a sequence point.

What is a full expression?

while (guests++ < 10)
printf("%d \n", guests);

A full expression is an expression that is not a subexpression of a larger expression.
The guests++ < 10 expression is a full expression because it is a while loop test condition, so the end of this expression is a sequence point. Therefor, C++ guarantees that the side effect (incrementing guests) takes place before the program moves on to printf(). Using the postfix form, however, guarantees that guests will be incremented after the comparison to 10 is made.

y = (4 + xx++) + (6 + x++);

The expression 4 + x++ is not a full expression, so C++ does not guarantee that x will be incremented immediately after the subexpression 4 + x++ is evaluated. Here the full expression is the entire assignment statement, and the semicolon marks the sequence point, so all that C++ guarantees is that x will have been incremented twice by the time the program moves to the next statement. C++ does not specify wether x is incremented after each subexpression is evaluated or only after all expressions have been evaluated, which is why you should AVOID statements of this kind!

Geen opmerkingen: