gpt4 book ai didi

c - C99 标准是否像 C++03 那样定义了可观察的行为?

转载 作者:行者123 更新时间:2023-12-02 01:49:50 26 4
gpt4 key购买 nike

在 C++03 标准 1.9/6 中有可观察行为的定义

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.

标准详细解释了编译器在进行优化时必须保留可观察到的行为

但是在我正在查看的 C99 草案中没有这样或类似的定义。唯一一次可观察到的行为被提及是 6.7.3/7

The intended use of the restrict qualifier (like the register storage class) is to promote optimization, and deleting all instances of the qualifier from a conforming program does not change its meaning (i.e., observable behavior)

是否有编译器在优化 C99 程序时必须保留的内容的定义?

最佳答案

在我的草案中,§3.4 将行为 定义为“外在表现或行动”。 “可观察到的行为”似乎是一个只发生一次的重复词。

§5.1.2.3,程序执行,进一步定义了 C 程序的行为:

The semantic descriptions in this International Standard describe the behavior of an abstract machine in which issues of optimization are irrelevant.

然后它继续将副作用定义为由“[a]访问 volatile 对象、修改对象、修改文件或调用执行任何操作的函数引起的“执行环境状态的变化”那些操作”。副作用按序列点排序。

这似乎比 C++ 更严格,因为“修改对象”,即写入内存,是 C 中的(可观察到的)行为。

至于允许的优化:

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

“需要的副作用”在以下几点列出:

  • At sequence points, volatile objects are stable in the sense that previous accesses are complete and subsequent accesses have not yet occurred.

  • At program termination, all data written into files shall be identical to the result that execution of the program according to the abstract semantics would have produced.

  • The input and output dynamics of interactive devices shall take place as specified in 7.19.3.

该段以示例列表结束; §7.19.3 描述了 stdio 上下文中的文件。

关于c - C99 标准是否像 C++03 那样定义了可观察的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com