tags:

views:

125

answers:

4

A sequence point in imperative programming defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.

What does this mean? Can somebody please explain it in simple words?

+12  A: 

When a sequence point occurs, it basically means that you are guaranteed that all previous operations are complete.

Changing a variable twice without an intervening sequence point is one example of undefined behaviour.

For example, i = i++; is undefined because there's no sequence point between the two changes to i.

Wikipedia has a list of the sequence points in the C and C++ standards although the definitive list should always be taken from the ISO standard. From C99 appendix C:


The following are the sequence points described in 5.1.2.3:

  • The call to a function, after the arguments have been evaluated (6.5.2.2).
  • The end of the first operand of the following operators: logical AND && (6.5.13); logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17).
  • The end of a full declarator: declarators (6.7.5);
  • The end of a full expression: an initializer (6.7.8); the expression in an expression statement (6.8.3); the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the expressions of a for statement (6.8.5.3); the expression in a return statement (6.8.6.4).
  • Immediately before a library function returns (7.1.4).
  • After the actions associated with each formatted input/output function conversion specifier (7.19.6, 7.24.2).
  • Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call (7.20.5).

C1X has the wording changed. It appears to have broken out the ternary operator and added some more detail.


The following are the sequence points described in 5.1.2.3:

  • Between the evaluations of the function designator and actual arguments in a function call and the actual call. (6.5.2.2).
  • Between the evaluations of the first and second operands of the following operators: logical AND && (6.5.13); logical OR || (6.5.14); comma , (6.5.17).
  • Between the evaluations of the first operand of the conditional ?: operator and whichever of the second and third operands is evaluated (6.5.15).
  • The end of a full declarator: declarators (6.7.6);
  • Between the evaluation of a full expression and the next full expression to be evaluated. The following are full expressions: an initializer (6.7.9); the expression in an expression statement (6.8.3); the controlling expression of a selection statement (if or switch) (6.8.4); the controlling expression of a while or do statement (6.8.5); each of the expressions of a for statement (6.8.5.3); the expression in a return statement (6.8.6.4).
  • Immediately before a library function returns (7.1.4).
  • After the actions associated with each formatted input/output function conversion specifier (7.21.6, 7.28.2).
  • Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call (7.22.5).
paxdiablo
@paxdiablo: I am expecting more information from you pax.I have basic knowledge in c.You gave very good explanation to my previous question.
Jagan
Woah, 100k rep! Congratulations.
dreamlax
It is also undefined behavior to modify the value of a variable and also use the value in any way other than to determine the value to be stored without an intervening sequence point. For example, a[i++] = i is undefined because, although it only modifies the value of i once, the value of i is used for a purpose other than to determine what value to store into i.
Robert Gamble
Any idea how this combines with OOE, or is that another level of abstraction?
Matt Joiner
@Matt: That's a layer of abstraction down. So long as you write correct C code, the result will be the same regardless of whether your platform does OOE or not. In fact, even if you write incorrect code, you should get the same result, because the binary executable is the same in each case.
Oli Charlesworth
+1  A: 

It means a compiler may do funky optimizations, tricks and magic but must reach a well-defined state at these so-called sequence points.

DarkDust
+1  A: 

Expanding on paxdiablo's answer with an example.

Assume the statement

x = i++ * ++j;

There are three side effects: assigning the result of i * (j+1) to x, adding 1 to i, and adding 1 to j. The order in which the side effects are applied is unspecified; i and j may each be incremented immediately after being evaluated, or they may not be incremented until after both have been evaluated but before x has been assigned, or they may not be incremented until after x has been assigned.

The sequence point is the point where all side effects have been applied (x, i, and j have all been updated), regardless of the order in which they were applied.

John Bode
However, we should point out that the result of `x = i++ * ++j` is well-defined, unlike paxdiablo's `i = i++` example...
Oli Charlesworth
+1  A: 

An important thing to note about sequence points is that they are not global, but rather should be regarded as a set of local constraints. For example, in the statement

a = f1(x++) + f2(y++);

There is a sequence point between the evaluation of x++ and the call to f1, and another sequence point between the evaluation of y++ and the call to f2. There is, however, no guarantee as to whether x will be incremented before or after f2 is called, nor whether y will be incremented before or after x is called. If f1 changes y or f2 changes x, the results will be undefined (it would be legitimate for the compiler's generated code to e.g. read x and y, increment x, call f1, check y against the previously-read value, and--if it changed--go on a rampage seeking out and destroying all Barney videos and merchandise; I don't think any real compilers generate code that would actually do that, alas, but it would be permitted under the standard).

supercat