In
C and
C++, a sequence point defines any point in a
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to Execution (computing), execute. It is one component of software, which also includes software documentation, documentation and other intangibl ...
's
execution
Capital punishment, also known as the death penalty and formerly called judicial homicide, is the state-sanctioned killing of a person as punishment for actual or supposed misconduct. The sentence ordering that an offender be punished in ...
at which it is guaranteed that all
side effect
In medicine, a side effect is an effect of the use of a medicinal drug or other treatment, usually adverse but sometimes beneficial, that is unintended. Herbal and traditional medicines also have side effects.
A drug or procedure usually use ...
s of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are a core concept for determining the validity of and, if valid, the possible results of expressions. Adding more sequence points is sometimes necessary to make an expression defined and to ensure a single valid order of evaluation.
With
C11 and
C++11
C++11 is a version of a joint technical standard, ISO/IEC 14882, by the International Organization for Standardization (ISO) and International Electrotechnical Commission (IEC), for the C++ programming language. C++11 replaced the prior vers ...
, usage of the term sequence point has been replaced by sequencing. There are three possibilities:
#An expression's evaluation can be ''sequenced before'' that of another expression, or equivalently the other expression's evaluation is ''sequenced after'' that of the first.
#The expressions' evaluation is ''indeterminately sequenced,'' meaning one is sequenced before the other, but which is unspecified.
#The expressions' evaluation is ''unsequenced.''
The execution of unsequenced evaluations can overlap, leading to potentially catastrophic
undefined behavior
In computer programming, a program exhibits undefined behavior (UB) when it contains, or is executing code for which its programming language specification does not mandate any specific requirements. This is different from unspecified behavior, ...
if they share
state
State most commonly refers to:
* State (polity), a centralized political organization that regulates law and society within a territory
**Sovereign state, a sovereign polity in international law, commonly referred to as a country
**Nation state, a ...
. This situation can arise in
parallel computations, causing
race condition
A race condition or race hazard is the condition of an electronics, software, or other system where the system's substantive behavior is dependent on the sequence or timing of other uncontrollable events, leading to unexpected or inconsistent ...
s, but undefined behavior can also result in single-threaded situations. For example,
a = i++;
(where is an array and is an integer) has undefined behavior.
Examples of ambiguity
Consider two
functions
f()
and
g()
. In C and C++, the
+
operator is not associated with 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 introduces a sequence point, and therefore in the code
f(),g()
the order of evaluation is defined: first
f()
is called, and then
g()
is called.
Sequence points also come into play when the same variable is modified more than once within a single expression. An often-cited example is the
C expression
i=i++
, which apparently both assigns
i
its previous value and increments
i
. The final value of
i
is ambiguous, because, depending on the order of expression evaluation, the increment may occur before, after, or interleaved with the assignment. The definition of a particular language might specify one of the possible behaviors or simply say the behavior is
undefined
Undefined may refer to:
Mathematics
*Undefined (mathematics), with several related meanings
**Indeterminate form, in calculus
Computing
*Undefined behavior, computer code whose behavior is not specified under certain conditions
*Undefined valu ...
. In C and C++, evaluating such an expression yields undefined behavior. Other languages, such as
C#, define the
precedence of the assignment and increment operator in such a way that the result of the expression
i=i++
is guaranteed.
Behavior
Up to C++03
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
In logic, mathematics and linguistics, ''and'' (\wedge) is the truth-functional operator of conjunction or logical conjunction. The logical connective of this operator is typically represented as \wedge or \& or K (prefix) or \times or \cdo ...
),
, ,
(
logical OR
In logic, disjunction (also known as logical disjunction, logical or, logical addition, or inclusive disjunction) is a logical connective typically notated as \lor and read aloud as "or". For instance, the English language, English language ...
) (as part of
short-circuit evaluation
Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argumen ...
), and
comma operator
In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there ...
s. For example, in the expression , all side effects of the sub-expression are completed before any attempt to access .
#Between the evaluation of the first operand of the
ternary conditional operator
In computer programming, the ternary conditional operator is a ternary operator that is part of the syntax for basic conditional expressions in several programming languages. It is commonly referred to as the conditional operator, conditional ...
and its second or third operand. For example, in the expression there is a sequence point after the first , 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 ),
return statement
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is sa ...
s, the controlling expressions of , , , or - statements, and each of the three expressions in a
for
For or FOR may refer to:
English language
*For, a preposition
*For, a complementizer
*For, a grammatical conjunction
Science and technology
* Fornax, a constellation
* for loop, a programming language statement
* Frame of reference, in physics
* ...
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 expression , is called with a parameter of the original value of , but is incremented before entering the body of . Similarly, and are updated before entering and respectively. However, it is not specified in which order , , are executed, nor in which order , , are incremented. If the body of accesses the variables and , it might find both, neither, or just one of them to have been incremented. (The function call is ''not'' a use of the comma operator; the order of evaluation for , , and 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.)
#At the end of an
initializer; for example, after the evaluation of in the declaration .
#Between each declarator in each declarator sequence; for example, between the two evaluations of in . (This is ''not'' an example of the comma operator.)
#After each conversion associated with an input/output format specifier. For example, in the expression , there is a sequence point after the is evaluated and before printing .
C11 and C++11
Partially because of the introduction of language support for threads, C11 and C++11 introduced new terminology for evaluation order. An operation may be "sequenced before" another, or the two can be "indeterminately" sequenced (one must complete before the other) or "unsequenced" (the operations in each expression may be interleaved).
C++17
C++17 restricted several aspects of evaluation order. The expression will always perform the memory allocation before evaluating the constructor arguments. The operators , , , , , and the subscript and function call operator are guaranteed to be evaluated left to right (whether they are overloaded or not). For example, the code
std::cout << a() << b() << c(); // parsed as (((std::cout << a()) << b()) << c());
is newly guaranteed to call , and in that order. The right-hand side of any assignment-like operator is evaluated before the left-hand side, so that
b() *= a();
is guaranteed to evaluate first. Finally, although the order in which function parameters are evaluated remains implementation-defined, the compiler is no longer allowed to interleave ''sub-expressions'' across multiple parameters.
References
External links
Question 3.8of the FAQ for
comp.lang.c
{{DEFAULTSORT:Sequence Point
C (programming language)
C++
Programming paradigms