...
This rule means that statements such as
Code Block | ||
---|---|---|
| ||
i = i + 1;
a[i] = i;
|
are allowed, while statements like
Code Block | ||
---|---|---|
| ||
i = ++i + 1; /* the value of i is modified twice between sequence points */ a[i++] = i; /* the value of i is read other than to determine the value to be stored */ |
are not allowed because they modify the same value twice.
Non-Compliant Code Example
Programs cannot safely rely on the order of evaluation of operands between sequence points. In this non-compliant code example, the order of evaluation of the operands to the +
is operator are unspecified.
Code Block | ||
---|---|---|
| ||
a = i + b[++i]; |
...
Code Block | ||
---|---|---|
| ||
a = 1 + b[1]; |
As a result, programs cannot safely rely on the order of evaluation of operands between sequence points.
Compliant Solution
These examples are independent of the order of evaluation of the operands and can only be interpreted in one way.
...
Code Block | ||
---|---|---|
| ||
a = i + b[i+1];
++i;
|
Non-Compliant Code Example
Both of these statements violate the rule concerning sequence points stated above, so the behavior of these statements is undefined.
Code Block | ||
---|---|---|
| ||
i = ++i + 1; /* an attempt is made to modify the value of i twice between sequence points */ a[i++] = i; /* an attempt is made to read the value of i other than to determine the value to be stored */ |
Compliant Solution
These statements are allowed by the standard.
Code Block | ||
---|---|---|
| ||
i = i + 1;
a[i] = i;
|
Non-Compliant Code Example
The order of evaluation for function arguments is unspecified.
...
The call to func()
has undefined behavior because there are no sequence points between the argument expressions. The first (left) argument expression reads the value of i
(to determine the value to be stored) and then modifies i
. 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.
...