Sequence point
≈ 5 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
In C and C++, a sequence point 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 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. Documentation for C11 and C++11 stopped using the term "sequence point" and now uses alternative terms: #An expression's evaluation can be "sequenced before" the evaluation of another expression. (Equivalently, the other expression's evaluation can be "sequenced after" that of the first.) #The expression's evaluation is "indeterminately sequenced", meaning that one is "sequenced before" the other, but which is unspecified. #The expression's evaluation is "unsequenced", meaning the operations in each expression may be interleaved. The execution of unsequenced evaluations can overlap, leading to potentially catastrophic undefined behavior if they share state. This situation can arise in parallel computing, causing race conditions, but undefined behavior can also result in single-threaded situations. For example, a[i] = i++; (where is an array and is an integer) has undefined behavior.…
In C and C++, a sequence point 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 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. Documentation for C11 and C++11 stopped using the term "sequence point" and now uses alternative terms: An expression's evaluation can be "sequenced before" the evaluation of another expression. (Equivalently, the other expression's evaluation can be "sequenced after" that of the first.) The expression's evaluation is "indeterminately sequenced", meaning that one is "sequenced before" the other, but which is unspecified. The expression's evaluation is "unsequenced", meaning the operations in each expression may be interleaved. The execution of unsequenced evaluations can overlap, leading to potentially catastrophic undefined behavior if they share state. This situation can arise in parallel computing, causing race conditions, but undefined behavior can also result in single-threaded situations. For example, a[i] = i++; (where is an array and is an integer) has undefined behavior. In earlier C standards (such as C89 and C99), sequence points were defined to occur at specific locations in code, including at the end of a full expression (such as a statement), after the evaluation of the first operand of the logical AND (&&) and logical OR (||) operators, after the first operand of the conditional (?:) operator, and at function call boundaries after all arguments have been evaluated. ==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. In C and C++, evaluating such an expression yields undefined behavior. Other languages, such as C#, define the precedence of the assignment operator and the increment operator in such a way that the result of the expression i=i++ is guaranteed. ==Behavior== ===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 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. ==See also== Memory barrier ==References== ==External links== Question 3.8 of the FAQ for comp.lang.c
Text derived from https://en.wikipedia.org/w/index.php?oldid=1352243099 under CC BY-SA 4.0; modifications have been made.