- Sequence point
A sequence point in
imperative programming defines any point in acomputer 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 andC++ , 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
f()
andg()
. In C and C++, the+
operator is not a sequence point, and therefore in the expressionf()+g()
it is possible that eitherf()
org()
will be executed first. The comma operator is a sequence point, and therefore in the codef(),g()
the order of evaluation is defined (i.e., firstf()
is called, and theng()
is called). The type and value of the whole expression are those ofg()
; the value off()
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 assignsi
to itself and incrementsi
; what is the final value ofi
? 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.equence points in C and C++
In C [Annex C of the
C99 specification lists the circumstances under which a sequence point may be assumed.] and C++ [The 1998 C++ standard lists sequence points for that language in section 1.9, paragraphs 16–18.] , 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++ != 0
are completed before any attempt to accessq
.
#Between the evaluation of the first operand of the ternary and the second or third operand. For example, in the expressiona = (*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 assignmenta=b;
),return statement s, the controlling expressions ofif
,switch
,while
, ordo
-while
statements, and all three expressions in afor
statement.
#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 expressionf(i++) + g(j++) + h(k++)
,f
is called with a parameter of the original value ofi
, buti
is incremented before entering the body off
. Similarly,j
andk
are updated before enteringg
andh
respectively. However, it is not specified in which orderf()
,g()
,h()
are executed, nor in which orderi
,j
,k
are incremented. The values ofj
andk
in the body off
are therefore undefined. [Clause 6.5#2 of theC99 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.] Note that a function callf(a,b,c)
is not a use of the comma operator and the order of evaluation fora
,b
, andc
is unspecified.
#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 [C++ standard, ISO 14882:2003, section 1.9, footnote 11.] .)
#At the end of an initializer; for example, after the evaluation of5
in the declarationint a = 5;
.References
* [http://c-faq.com/expr/seqpoints.html Question 3.8] of the FAQ for
comp.lang.c
Wikimedia Foundation. 2010.