HOME

TheInfoList



OR:

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. Computer programs are one component of software, which also includes software documentation, documentation and oth ...
's
execution Capital punishment, also known as the death penalty, is the state-sanctioned practice of deliberately killing a person as a punishment for an actual or supposed crime, usually following an authorized, rule-governed process to conclude that ...
at which it is guaranteed that all
side effect In medicine, a side effect is an effect, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequence ...
s 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++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, because they are a core concept for determining the validity 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
C++11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build ...
, 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, with catastrophic
undefined behavior In computer programming, undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres. This is different from unspecified behavior ...
if they share
state State may refer to: Arts, entertainment, and media Literature * ''State Magazine'', a monthly magazine published by the U.S. Department of State * ''The State'' (newspaper), a daily newspaper in Columbia, South Carolina, United States * ''Our S ...
. This situation can arise in
parallel computation Parallel computing is a type of computation in which many calculations or processes are carried out simultaneously. Large problems can often be divided into smaller ones, which can then be solved at the same time. There are several different fo ...
s, 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 Sequential logic, dependent on the sequence or timing of other uncontrollable events. It becomes a software ...
s. However, it can already arise in simple non-concurrent situations like (a = 1) + (b = a), where part of the assignment to a (e.g., half of the bits) may happen before b = a, and the rest afterwards, such that after evaluation of the expression, b may contain a meaningless intermediate state of a.


Examples of ambiguity

Consider two
function Function or functionality may refer to: Computing * Function key, a type of key on computer keyboards * Function model, a structured representation of processes in a system * Function object or functor or functionoid, a concept of object-oriente ...
s f() and g(). In C and C++, the + operator is not associated with a sequence point, and therefore in the
expression Expression may refer to: Linguistics * Expression (linguistics), a word, phrase, or sentence * Fixed expression, a form of words with a specific meaning * Idiom, a type of fixed expression * Metaphorical expression, a particular word, phrase, o ...
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 ...
. In C and C++, evaluating such an expression yields undefined behavior. Other languages, such as C#, define the
precedence Precedence may refer to: * Message precedence of military communications traffic * Order of precedence, the ceremonial hierarchy within a nation or state * Order of operations, in mathematics and computer programming * Precedence Entertainment, ...
of the assignment and increment operator in such a way that the result of the expression i=i++ is guaranteed.


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 In logic, mathematics and linguistics, And (\wedge) is the truth-functional operator of logical conjunction; the ''and'' of a set of operands is true if and only if ''all'' of its operands are true. The logical connective that represents this ...
), , , (
logical OR In logic, disjunction is a logical connective typically notated as \lor and read aloud as "or". For instance, the English language sentence "it is raining or it is snowing" can be represented in logic using the disjunctive formula R \lor S ...
) (as part of
short-circuit evaluation A short circuit (sometimes abbreviated to short or s/c) is an electrical circuit that allows a current to travel along an unintended path with no or very low electrical impedance. This results in an excessive current flowing through the circuit. ...
), and comma operators. 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, ternary if, or ...
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 sav ...
s, the controlling expressions of , , , or - statements, and each of the three expressions in a for 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 .C++ standard, ISO 14882:2003, section 8.3: "Each init-declarator in a declaration is analyzed separately as if it was in a declaration by itself." (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 .


References


External links


Question 3.8
of the FAQ for comp.lang.c {{DEFAULTSORT:Sequence Point C (programming language) C++ Programming paradigms