Sequence points and side effects: Quiet change in C11?

前端 未结 4 1106
天涯浪人
天涯浪人 2020-12-31 06:42

C99 §6.5 Expressions

(1) An expression is a sequence of operators and operands that specifies computation of a value, or that designa

4条回答
  •  感情败类
    2020-12-31 07:05

    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:

    1. Between the previous and next sequence point

    2. an object

    3. shall have

    4. its stored value modified at most once

    5. 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.

提交回复
热议问题