Sequence points and side effects: Quiet change in C11?

c, c11, c99, sequence-points, undefined-behavior

Solution

C11 (and also C++11) has completely reworked the wording of sequencing because C11 now has threads, and it had to explain what sequencing between threads that access the same data means. The intention of the committee was to keep things backward compatible to C99 for the case where there is only one thread of execution.

Let's have a look at the C99 version:

Between the previous and next sequence point

an object

shall have

its stored value modified at most once

by the evaluation of an expression.

compared to the new text

If a side effect on

different terminolgie for 4, modifying the stored value

a scalar object

a restriction of the previous wording in 2. The new text only says something about scalar objects

is unsequenced relative to either

unsequenced is a generalization of the concept in 1. that two statements were separated by a sequence point. Think of two threads that modify the same data without using a lock or something similar.

a different side effect on the same scalar object

the object is only allowed be modified once

or a value computation using the value of the same scalar object,

or a read of the value may not appear concurrently to the modification

the behavior is undefined.

The "shall" in 3. is saying this implicitly. All "shall"s lead to UB if they are not fulfilled.

Problem

C99 §6.5 Expressions (1) An expression is a sequence of operators and operands that specifies computation of a value, or that designates an object or a function, or that generates side effects, or that performs a combination thereof. (2) Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.72) Furthermore, the prior value shall be read only to determine the value to be stored.73) with the footnotes 72) A floating-point status flag is not an object and can be set more than once within an expression. 73) This paragraph renders undefined statement expressions such as ``` i = ++i + 1; a[i++] = i; ``` while allowing ``` i = i + 1; a[i] = i; ``` where C11 §6.5 changed to (the text of (1) has an addendum): (1) […] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. (2) If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. If there are multiple allowable orderings of the subexpressions of an expression, the behavior is undefined if such an unsequenced side effect occurs in any of the orderings.84) where footnote 84 in C11 is the same as 73 in C99. I'm a little confused… I read C11 (2) as "[…] either (a different side effect on the same scalar object) or (a value computation using the value of the same scalar object) […]" which seems to not even allow `foo = ++i` (there is a side effect and we use a value depending on the changed object). I'm not a native speaker, though, so it would be nice if one could tell me how this sentence should be "parsed". I understand C99, but I don't quite understand the wording of C11. Anyway, the actual question: Is this a change from C99 to C11, or are these wordings equivalent? And if so, why it has been changed? And if not, could someone give an example of an expression which is UB in C99 but not in C11 or vice versa?

Original source