|
A sequence point in a programming language 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 often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation (adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation). Computer code (HTML with JavaScript) in a tool that uses syntax highlighting (colors) to help the developer see the purpose of each piece of code. ...
A computer program or software program (usually abbreviated to a program) is a step-by-step list of instructions written for a particular computer architecture in a particular computer programming language. ...
In computer science, a side-effect is a property of a programming language function that it modifies some state other than its return value. ...
The C Programming Language, Brian Kernighan and Dennis Ritchie, the original edition that served for many years as an informal specification of the language The C programming language is a standardized imperative computer programming language developed in the early 1970s by Dennis Ritchie for use on the Unix operating system. ...
C++ (generally pronounced see plus plus, IPA: ) is a general-purpose computer programming language. ...
Examples of ambiguity Consider two functions f() and g(). The '+' operator is not 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 (',') is a sequence point, and therefore in "f(),g()" the order of evaluation is defined (ie, first f(), and then g() is called). The type and value of the expression is that of g() while the value of f() is discarded. In computer science, a subroutine (function, procedure, or subprogram) is a sequence of code which performs a specific task, as part of a larger program, and is grouped as one or more statement blocks; such code is sometimes collected into software libraries. ...
An expression in a programming language is a combination of values and functions or procedures, interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then returns another value. ...
Sequence points also come into play when the same variable is modified more than once. An often quoted example is the expression "i=i++", which both assigns i to itself and increments i; what is the final value of i? Language definitions might specify one of the possible behaviors or simply say the behavior is undefined (the C and C++ specifications only defines the behavior if the same variable is modified at most once between two sequence points).
Sequence points in C and C++ - Between evaluation of the left and right operands of the && (logical AND), || (logical OR), comma operator. For example, in the expression "*p++ != 0 && *q++ != 0", all side effects of the sub-expression *p++ != 0 are completed before any attempt to access q.
- Between the evaluation of first operand of the ternary operator ( ? : ) and the second or third operand. For example, in the expression "a = (*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 an expression statement ( ; ).
- Before a function is entered in a function call. The order in which the arguments are evaluated is not defined, but this sequence point just means that all of their side effects are complete before the function is entered. In the expression "f(i++) + g(j++) + h(k++)", f is called with a parameter of the original value of i, but i is incremented before entering the body of f. Similarly for j and k before entering g and h respectively. However, it is not defined in which order f(), g(), h() are executed, nor in which order i, j, k are incremented. The value of j and k in the body of f is therefore undefined.
- After the controlling expression is evaluated in an if/switch statement.
- After the controlling expression is evaluated in a while or do-while statement.
- After each of the 3 operands of the for statement is evaluated.
- At the end of a return statement.
- At the end of an initializer.
|