Rudra Dutta CSC 230 - Spring 2007, Section 001 Sequence Points Rudra Dutta CSC 230 - Spring 2007, Section 001
Sequence of Instructions Program consists of series of instructions Source code is specification of not only the instructions to execute but the sequence of execution However, “single” instructions of source code become sets of multiple actual instructions Even more so for a compact language such as C There is a limit to the power of specification of instruction sequence Formalized in the concept of “sequence points” Certain points in the execution sequence where all side effects of previous evaluations shall be complete, and no side effects of subsequent evaluations shall have taken place Copyright Rudra Dutta, NCSU, Spring, 2007
Sequence Points The call to a function, after the arguments have been evaluated The end of the first operand of the following operators: logical AND && logical OR || conditional ? comma , The end of a full declarator A declarator is the part of a declaration (definition or initialization) that is not the type Copyright Rudra Dutta, NCSU, Spring, 2007
Sequence Points The end of a full expression: an initializer the expression in an expression statement the controlling expression of a selection statement (if or switch) the controlling expression of a while or do statement each of the expressions of a for statement the expression in a return statement Immediately before a library function returns After the actions associated with each formatted input/output function conversion specifier Immediately before and immediately after each call to a comparison function, and also between any call to a comparison function and any movement of the objects passed as arguments to that call Copyright Rudra Dutta, NCSU, Spring, 2007
Example of Sequence Point Issue #include <stdio.h> #include <stdlib.h> int j; void func_1 (int i, int k) { for (j=0; j<i; j++) { printf ("%d ", j); } printf ("%d\n", k); int main () { int j; for (j=0; j<10; j++) { func_1 (j, j); } Copyright Rudra Dutta, NCSU, Spring, 2007
Example of Sequence Point Issue #include <stdio.h> #include <stdlib.h> int j; void func_1 (int i, int k) { for (j=0; j<i; j++) { printf ("%d ", j); } printf ("%d\n", k); int main () { /* int j; */ for (j=0; j<10; j++) { func_1 (j, j); } Copyright Rudra Dutta, NCSU, Spring, 2007
Example of Sequence Point Issue #include <stdio.h> #include <stdlib.h> int j; void func_1 (int i, int k) { /* int j; */ for (j=0; j<i; j++) { printf ("%d ", j); } printf ("%d\n", k); int main () { /* int j; */ for (j=0; j<10; j++) { func_1 (j, j); } Copyright Rudra Dutta, NCSU, Spring, 2007
Example of Sequence Point Issue #include <stdio.h> #include <stdlib.h> int j; void func_1 (int i, int k) { /* int j; */ for (j=0; j<i; j++) { printf ("%d ", j); } printf ("%d\n", k); int main () { /* int j; */ for (j=0; j<10; j++) { func_1 (++j, j); } Copyright Rudra Dutta, NCSU, Spring, 2007
Example of Sequence Point Issue #include <stdio.h> #include <stdlib.h> int j; void func_1 (int i, int k) { /* int j; */ for (j=0; j<i; j++) { printf ("%d ", j); } printf ("%d\n", k); int main () { /* int j; */ for (j=0; j<10; j++) { func_1 (j, ++j); } Copyright Rudra Dutta, NCSU, Spring, 2007
Expressions and Sequence Points Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored. i = i + 1; a[i] = i; i = ++i + 1; a[i++] = i; Copyright Rudra Dutta, NCSU, Spring, 2007
Function Calls and Sequence Points The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified BUT there is a sequence point before the actual call Copyright Rudra Dutta, NCSU, Spring, 2007