C运算符+ =顺序点? [英] C operator += Sequence point?

查看:268
本文介绍了C运算符+ =顺序点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是定义的行为?

*p += *p--;

和,如果是,是什么相当于 {P [0] + = P [0]; --P; } {P [-1] = P [0]; --P; }

And, if it is, is it equivalent to { p[0] += p[0]; --p; } or to { p[-1] = p[0]; --p; } ?

我猜被定义与否取决于 + = 是否有一个隐含的顺序点,如果有,我的猜测是,第二块应正确的。

I'm guessing the being defined or not depends on whether += has an implicit sequence point and, if it has, my guess is that the second block should be the correct one.

编辑:我想是因为主要的问题有哪些序列点,怎么办影响行为这不是建议的问题的副本。在我来说,我有一个序列的一点是什么明确的想法和问题的专门的关于 + = 经营者是否有一个隐含的顺序点或没有。

I think it's not a duplicate of the suggested question because the main question there is what are sequence points and how do the affect behaviour. In my case I have clear idea of what a sequence point is and the question is specifically on whether the += operator has an implicit sequence point or not.

推荐答案

这是不确定的行为,因为计算结果为 * P 是unsequenced在相关<评价code> * p - 。有没有顺序点。对于所有的赋值运算符,6.5.16:

It is undefined behavior because the evaulation of *p is unsequenced in related to the evaluation of *p--. There is no sequence point. For all assignment operators, 6.5.16:

更新所述左操作数的存储值的副作用是
  左和右操作数的值计算后进行测序。
  操作数的评价是unsequenced。

The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.

6.5的状态,这是UB:

6.5 states that it is UB:

如果一个标物体上的副作用是相对于unsequenced任
  同样标对象或值在不同的副作用
  使用同一个标量对象的值计算,该行为是
  未定义。如果有的多个容许序
  SUBEX $一名前pression的对$ pssions,行为是不确定的,如果这样的
  unsequenced副作用发生在任何一个排序的。

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.

这篇关于C运算符+ =顺序点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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