为什么" volatileQualifiedExpr + volatileQualifiedExpr"不一定UB在C,但在C ++? [英] Why is "volatileQualifiedExpr + volatileQualifiedExpr" not necessarily UB in C but in C++?

查看:110
本文介绍了为什么" volatileQualifiedExpr + volatileQualifiedExpr"不一定UB在C,但在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我今天看了C标准,它说,有关的副作用

When I today read the C Standard, it says about side effects

访问的易失性对象,修改对象,修改文件或调用函数
  ,做任何这些操作都是副作用

Accessing a volatile object, modifying an object, modifying a file, or calling a function that does any of those operations are all side effects

和C ++标准说

访问由挥发性glvalue(3.10)指定的对象,修改对象,调用库I / O功能,或调用任何做这些操作都是副作用的功能

Accessing an object designated by a volatile glvalue (3.10), modifying an object, calling a library I/O function, or calling a function that does any of those operations are all side effects

因此​​,因为两者禁止unsequenced副作用同一标对象上发生,C允许以下,但C ++使得未定义行为

Hence because both forbid unsequenced side effects to occur on the same scalar object, C allows the following, but C++ makes it undefined behavior

int a = 0;
volatile int *pa = &a;

int b = *pa + *pa;

是我读的规格是否正确?什么是差异的原因,如果是这样?

Am I reading the specifications correctly? And what is the reason for the discrepancy, if so?

推荐答案

我不相信有这方面C和C ++之间的有效差异。虽然测序的措辞变化的最终结果是一样的:都导致不确定的行为(既是C似乎表明该评估将suceed但未定义的结果)

I don't believe there is an effective variation between C and C++ in this regards. Though the wording on sequencing varies the end result is the same: both result in undefined behaviour (though C seems to indicate the evaluation will suceed but with an undefined result).

在C99(对不起,没有C11方便)段5.1.2.3.5指定:

In C99 (sorry, don't have C11 handy) paragraph 5.1.2.3.5 specifies:

- 在序列点,挥发性对象是在previous访问是稳定感
  尚未发生完成,后续访问。

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

与引自5.1.2.3.2组合将表明的值每年不会在为访问的至少一个稳定的状态,以每年。这使得逻辑意义,因为编译器将被允许以任何顺序来评估它们,只需一次,或在同一时间(如果可能)。它实际上并没有界定什么的稳定的不过意思。

Combined with your quote from 5.1.2.3.2 would indicate the value of pa would not be in a stable state for at least one of the accesses to pa. This makes logical sense since the compiler would be allowed to evaluate them in any order, just once, or at the same time (if possible). It doesn't actually define what stable means however.

在C ++ 11没有明确提及unsequenced oeprations在1.9.13。然后15点表明这种unsequenced操作在同一操作数是不确定的。由于的未定义行为的可能仅仅是发生这也许是比C的的不稳定的行为强烈。然而,在两种情况下,你的前妻pression不保证结果。

In C++11 there is explicit reference to unsequenced oeprations at 1.9.13. Then point 15 indicates such unsequenced operations on the same operand is undefined. Since undefined behaviour can mean anything happens it is perhaps strong than C's unstable behaviour. However, in both cases there is no guaranteed result of your expression.

这篇关于为什么" volatileQualifiedExpr + volatileQualifiedExpr"不一定UB在C,但在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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