...
* Note that not all instances of a comma in C code denote a usage of the comma operator. E.g., the comma in passing arguments is NOT the comma operator.
According to C99:
Between the previous and next sequence point an object can only have its stored value modified once by the evaluation of an expression. Additionally, the prior value can be read only to determine the value to be stored.
This rule means that statements such as
...
Non-Compliant Code Example
The order of evaluation of for function arguments to a function is undefinedunspecified.
Code Block | ||
---|---|---|
| ||
func(i++, i++);
|
The call to func()
has undefined behavior because there's no sequence point between the argument expressions. The first (left) argument modifies i
. It also reads the value of i
, but only to determine the new value to be stored in i
. So far, so good. However, the second (right) argument expression reads the value of i
between the same pair of sequence points as the first argument, but not to determine the value to be stored in i
. This additional attempt to read the value of i
has undefined behavior.
Compliant Solution
This solution is appropriate when the programmer intends for both arguments to func()
to be equivalent.
...
Wiki Markup |
---|
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 5.1.2.3, "Program execution"
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.5, "Expressions"
\[[ISO/IEC 9899-1999|AA. C References#ISO/IEC 9899-1999]\] Annex C, "Sequence points"
\[[Summit 05|AA. C References#Summit 05]\] Questions 3.1, 3.2, 3.3, 3.3b, 3.7, 3.8, 3.9, 3.10a, 3.10b, 3.11
Dan Saks. Sequence Points Embedded Systems Design. 07/01/02. http://www.embedded.com/columns/programmingpointers/9900661?_requestid=481957 |