Sequence points in c

The point of sequence in imperative programming defines any point in the execution of a computer program at which it is guaranteed that all side effects of previous evaluations are fulfilled and no side effects from subsequent evaluations have yet been performed.

What does it mean? Can someone explain this in simple words?

+24
c sequence-points
Aug 26 '10 at 13:10
source share
4 answers

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

Changing a variable twice without an intermediate point in the sequence is one example of undefined behavior.

For example, i = i++; - undefined because there is no sequence point i between these two changes.

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




Below are the sequence points described in 5.1.2.3:

  • A function call after evaluating the arguments (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).
  • End of the full declarator: declarators (6.7.5);
  • End of full expression: initializer (6.7.8); expression in expression statement (6.8.3); control expression of the selection operator (if or switch) (6.8.4); control expression while or do (6.8.5); each of the for statement (6.8.5.3); expression in the return statement (6.8.6.4).
  • Just before the library function returns (7.1.4).
  • After the actions associated with each conversion of formatted input / output functions, the specifier (7.19.6, 7.24.2).
  • Immediately before and immediately after each call to the comparison function and also between any call to the comparison function and any movement of objects passed as arguments for this call (7.20.5).



C11 changed the wording. It looks like he broke the ternary operator and added a few details:




Below are the sequence points described in 5.1.2.3:

  • Between evaluations of the function name and actual arguments in the call function and the actual call. (6.5.2.2).
  • Between the estimates 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 estimates of the first operand of the conditional ?: operator and which of the second and third operands is evaluated (6.5.15).
  • End of the full declarator: declarators (6.7.6);
  • Between the evaluation of the full expression and the next full expression to be evaluated. The following are the full expressions: initializer (6.7.9); expression to expression (6.8.3); a control expression of a choice expression (if or a switch) (6.8.4); control expression while or do statement (6.8.5); each of the expressions of the for statement (6.8.5.3); expression in return (6.8.6.4).
  • Just before the library function returns (7.1.4).
  • After the actions associated with each conversion of formatted input / output functions, the specifier (7.21.6, 7.28.2).
  • Immediately before and immediately after each call to the comparison function and also between any call to the comparison function and any movement of objects passed as arguments for this call (7.22.5).
+36
Aug 26 '10 at 13:13
source share

It is important to note that sequence points are not global, but rather should be considered as a set of local constraints. For example, in a statement

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

There is a sequence point between evaluating x ++ and calling f1 and another point of sequence between evaluating y ++ and calling f2. However, there is no guarantee as to whether x will increase before or after calling f2, and whether y will increase before or after calling x. If f1 changes y or f2, change x, the results will be undefined (for code generated by the compiler, it would be legal, for example, to read x and y, increment x, call f1, check y for a previously read value, and if it changes, go rage, search and destroy all Barney’s videos and products, I don’t think that any real compilers generate code that would really do this, alas, but that would be allowed by standard).

+7
Aug 26 '10 at 15:52
source share

Turning to paxdiablo, answer an example.

Assume the statement

 x = i++ * ++j; 

There are three side effects: assigning the result i * (j+1) x, adding 1 to i and adding 1 to j. The order of use of side effects is unspecified; i and j can be increased immediately after the evaluation, or they cannot be increased until both are evaluated, but before x is assigned, or they cannot be increased until x is assigned .

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

+3
Aug 26 '10 at 15:10
source share

This means that the compiler can do funky optimizations, tricks and magic, but must achieve a well-defined state at these so-called points in the sequence.

+1
Aug 26 '10 at 13:18
source share



All Articles