求值顺序:C 中的子表达式、序列点和后缀增量 [英] Order of evaluation: subexpressions, sequence points and postfix increments in C

查看:100
本文介绍了求值顺序:C 中的子表达式、序列点和后缀增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

围绕 C 语句 x = b[i] + i++; 及其定义性展开了讨论.

A discussion arose around the C statement x = b[i] + i++; and its definedness.

未定义所述语句的参数如下:

The argument for said statement to be undefined goes something like this:

C99 的第 6.5 节规定:

§ 6.5 of C99 states:

[…] 子表达式的计算顺序和副作用发生的顺序都是未指定的.

[…] the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

因此不能保证 i 在下标运算符中用作数组索引后递增.

Thus it is not guaranteed that i is incremented after it is used in the subscript operator as index of the array.

但是,我对上述规范的解释不同.

However, I interpret said specification differently.

C99 的第 6.5 节另外指出:

§ 6.5 of C99 additionally states:

在上一个和下一个序列点之间,一个对象应该有它的存储值通过对表达式的评估最多修改一次.此外,先验值应只读以确定要存储的值.

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

C99 的第 5.1.2.3 节规定:

§ 5.1.2.3 of C99 states:

在执行序列中的某些指定点称为序列点,所有副作用之前的评估应该是完整的,并且没有后续评估的副作用应该已经发生了.

At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.

附件 C 中给出了序列点列表,只有以下内容与恕我直言中的陈述相匹配.

A list of sequence points is given in annex C and only the following matches the statement in question IMHO.

完整表达式的结尾

b[i]的求值(b的元素i的值)和i++的求值>(只是 i)可以在添加(以及对 = 的评估,即 RHS 的值)完成之前以任何顺序发生.然而,整个语句的副作用被推迟到所有这些评估之后,因为这是唯一的序列点.在这种情况下,副作用是 x 的变化和 i 的增量.

The evaluation of b[i] (the value of element i of b) and that of i++ (just i) can happen in any order before the addition (and evaluation of =, which is the value of the RHS) is done. However, the side effects of the whole statement are deferred until after all these evaluations because that's the only sequence point. In this case the side effects are the change of x and the increment of i.

谁是对的?是否有与论证相关的额外段落?在 C++ 中有什么不同吗?

Who is right? Are there additional paragraphs relevant for the argument? Is it any different in C++?

推荐答案

您在 6.5 节中的引用是相关的:

Your quotation from section 6.5 is the relevant one:

在上一个和下一个序列点之间,一个对象应该有它的存储的值最多被表达式的评估修改一次.[在那种情况下,f]此外,先验值应读取 [这些序列点之间] 仅用于确定值存储.

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. [In that event, f]urthermore, the prior value shall be read [between those sequence points] only to determine the value to be stored.

(我的澄清.)

在您的语句中,i 的值被修改并用作b 的索引.您的语句不包含内部序列点,因此这些效果必须发生在同一对序列点之间.因此,该声明违反了引用的要求.第 4 节第 2 段适用:

In your statement, the value of i is both modified and used as an index into b. Your statement contains no internal sequence points, so these effects must occur between the same pair of sequence points. The statement therefore violates the quoted requirement. Section 4, paragraph 2 then applies:

如果应该"或不应"要求出现在违反约束,行为未定义.[...]

If a ''shall'' or ''shall not'' requirement that appears outside of a constraint is violated, the behavior is undefined. [...]

仅此而已.不需要其他考虑.您关于实际操作顺序的论点完全无关紧要.

That's all there is to it. No other considerations are required. Your argument about actual order of operations is completely irrelevant.

尽管如此,您声称

整个语句的副作用被推迟到毕竟这些评估是因为这是唯一的序列点.

the side effects of the whole statement are deferred until after all these evaluations because that's the only sequence point.

反映了对序列点的严重误解.序列点不代表事情发生的时间何时,而是代表事情发生的边界.副作用不仅不会推迟到下一个序列点,而且与计算表达式值所涉及的操作相比,它们受到的约束(按标准)要少得多.

reflects a serious misunderstanding of sequence points. Sequence points do not represent times when things happen, but rather boundaries between which things happen. Not only are side effects not deferred to the next sequence point, they are far less constrained (by the standard) than operations involved in computing the values of expressions.

这篇关于求值顺序:C 中的子表达式、序列点和后缀增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆