Opentopia Directory Encyclopedia Tools

Sequence point

Encyclopedia : S : SE : SEQ : Sequence point


A sequence point in a programming language 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. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation (adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation).

Examples of ambiguity

Consider two functions f() and g(). The '+' operator is not a sequence point and therefore in the expression "f()+g()", it is possible that either f() or g() will be executed first. The comma operator (',') is a sequence point, and therefore in "f(),g()" the order of evaluation is defined (ie, first f(), and then g() is called). The type and value of the expression is that of g() while the value of f() is discarded.

Sequence points also come into play when the same variable is modified more than once. An often quoted example is the expression "i=i++", which both assigns i to itself and increments i; what is the final value of i? Language definitions might specify one of the possible behaviors or simply say the behavior is undefined (the C and C++ specifications only defines the behavior if the same variable is modified at most once between two sequence points).

Sequence points in C and C++

  1. Between evaluation of the left and right operands of the && (logical AND), || (logical OR), comma operator. For example, in the expression "*p++ != 0 && *q++ != 0", all side effects of the sub-expression *p++ != 0 are completed before any attempt to access q.
  2. Between the evaluation of first operand of the ternary operator ( ? : ) and the second or third operand. For example, in the expression "a = (*p++) ? (*p++) : 0" there is a sequence point after the first *p++, meaning it has already been incremented by the time the second instance is executed.
  3. At the end of an expression statement ( ; ).
  4. Before a function is entered in a function call. The order in which the arguments are evaluated is not defined, but this sequence point just means that all of their side effects are complete before the function is entered. In the expression "f(i++) + g(j++) + h(k++)", f is called with a parameter of the original value of i, but i is incremented before entering the body of f. Similarly for j and k before entering g and h respectively. However, it is not defined in which order f(), g(), h() are executed, nor in which order i, j, k are incremented. The value of j and k in the body of f is therefore undefined.
  5. After the controlling expression is evaluated in an if/switch statement.
  6. After the controlling expression is evaluated in a while or do-while statement.
  7. After each of the 3 operands of the for statement is evaluated.
  8. At the end of a return statement.
  9. At the end of an initializer.

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: