What Is a Sequence Point?

Sequence points, also called sequence points, are execution points in a computer program where all the side effects of evaluation before it have occurred and all side effects of evaluation after it have not yet started. In C and C ++ programming languages, the value of an expression depends on the order in which its subexpressions are evaluated. Adding more sequence points limits the possible evaluation order and guarantees a consistent result.

In languages such as C and C ++ that allow expressions to have side effects, order points are usually specified. The order point is the latest effective time of the side effect in the expression, or a key point in program execution. Before this sequence point, all the calculation of side effects must be completed. It can be said that the sequence point is the watershed of the previous stage calculation [1]
Expressions are also called expressions, arithmetic expressions, or mathematical expressions. In the field of mathematics, some symbols are limited and well-defined combinations according to the rules of the context. Mathematical symbols can be used to scale numbers (constants), variables, operations, functions, parentheses, punctuation, and groupings to help determine the order of operations and other logical syntax considerations. Expressions have two functions:
Expressions must produce a value
Expressions can have "side effects."
  • For example, the return value of i ++ is i, and the side effect is that i increases by 1.
  • For example, the return value of ++ i is i + 1, and the side effect also causes i to increase by 1.
When the side effects in the expression take effect and the evaluation order of the operands will affect the value of the expression. such as:
  int a = 4, b;
 b = a ++ + a;

If two operands of "+" are calculated from left to right:
  • If the side effect of the postfix increment operator is already in effect when calculating the second operand "a", then the value of b is 9;
  • If the side effect of the suffix increment operator takes effect when entering the "+" operation, then the value of b is 8.
If the two operands of "+" are calculated from right to left, the value of b is 8.
The C ++ standard states that side effects are accessing an object (basic.lval) assigned by a volatile lvalue, modifying an object, calling a library I / O function, or calling a function. All of these can change the execution environment. State operations are side effects.
In C and C ++, the order point appears in the following places: (C ++ overloaded operators behave like functions)
  1. && (logical AND), || (logical OR), between the left and right operand evaluation of the comma operator (the first two are part of the short-circuit evaluation). For example, the expression * p ++! = 0 && * q ++! = 0, and the side effects of the subexpression * p ++! = 0 will be completed before trying to access q.
  2. The ternary conditional operator is after the first operand and before the second or third operand. For example, the expression a = (* p ++)? (* P ++): 0 has a sequence point after the first * p ++, so it has already been incremented once before the second * p ++ is evaluated.
  3. The end of the full expression. Including expression statements (such as assignment a = b;), return statements, control expressions of if, switch, while, and do-while statements, and three expressions of the for statement.
  4. Function entry point at function call. The order in which the function arguments are evaluated is not specified, but the order point means that the side effects of the evaluation of these arguments have been completed when entering the function. The expression f (i ++) + g (j ++) + h (k ++), the order of calling f (), g (), h () is not specified, and the auto-increment order of i, j, and k is not specified. The argument list of the function call f (a, b, c) is not a comma operator, and the evaluation order of a, b, and c is not specified.
  5. When the function returns, the return value has been copied to the calling context. (Only the C ++ standard states this order point [6])
  6. End of initialization. For example, after evaluating 5 in the statement int a = 5 ;.
  7. Each comma-separated initialization value of the initialization list is strictly evaluated from left to right. For example: int a [3] = {i ++, j-, foo (101)}; Note that this is not a comma operator. (Point out this order from the C ++ 11 standard)
  8. Between each declarator in the declaration sequence. For example, int x = a ++, y = a ++ between two a ++ evaluations. [7] Note that this example is not a comma operator.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?