From Seo Wiki - Search Engine Optimization and Programming Languages
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. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. 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
g(). In C and C++, the
+ operator is not a sequence point, and therefore in the expression
f()+g() it is possible that either
g() will be executed first. The comma operator is a sequence point, and therefore in the code
f(),g() the order of evaluation is defined (i.e., first
f() is called, and then
g() is called). The type and value of the whole expression are those of
g(); the value of
f() is discarded.
Sequence points also come into play when the same variable is modified more than once. An often-cited 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. In C and C++, evaluating such an expression yields undefined behavior.
Sequence points in C and C++
In C and C++, sequence points occur in the following places. (In C++, overloaded operators act like functions, and thus operators that have been overloaded introduce sequence points in the same way as function calls.)
- Between evaluation of the left and right operands of the && (logical AND), || (logical OR), and comma operators. For example, in the expression
*p++ != 0 && *q++ != 0, all side effects of the sub-expression
*p++ != 0are completed before any attempt to access
- Between the evaluation of the first operand of the ternary "question-mark" operator and the second or third operand. For example, in the expression <code>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.
- At the end of a full expression. This category includes expression statements (such as the assignment
a=b;), return statements, the controlling expressions of
whilestatements, and all three expressions in a
- Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered. In the expression
f(i++) + g(j++) + h(k++),
fis called with a parameter of the original value of
iis incremented before entering the body of
kare updated before entering
hrespectively. However, it is not specified in which order
h()are executed, nor in which order
kare incremented. The values of
kin the body of
fare therefore undefined. Note that a function call
f(a,b,c)is not a use of the comma operator and the order of evaluation for
- At a function return, after the return value is copied into the calling context. (This sequence point is only specified in the C++ standard; it is present only implicitly in C.)
- At the end of an initializer; for example, after the evaluation of
5in the declaration
int a = 5;.
- ↑ Annex C of the C99 specification lists the circumstances under which a sequence point may be assumed.
- ↑ The 1998 C++ standard lists sequence points for that language in section 1.9, paragraphs 16–18.
- ↑ Clause 6.5#2 of the C99 specification: "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored." Accessing the value of j inside f therefore invokes undefined behavior.
- ↑ C++ standard, ISO 14882:2003, section 1.9, footnote 11.