c – 可以将volatile volatile的间接变化视为未定义的行为吗?

c – 可以将volatile volatile的间接变化视为未定义的行为吗?,第1张

概述易失性写入易失性const是否会引入未定义的行为?如果我在写作时放弃挥发怎么办? volatile const int x = 42;const volatile int *p = &x;*(volatile int *)p = 8; // Does this line introduce undefined behavior?*(int *)p = 16; // And what abou 易失性写入易失性const是否会引入未定义的行为?如果我在写作时放弃挥发怎么办?

volatile const int x = 42;const volatile int *p = &x;*(volatile int *)p = 8; // Does this line introduce undefined behavior?*(int *)p = 16; // And what about this one?

Compilable code

解决方法 当您尝试修改“初始”const对象时,它是未定义的行为(对于两个语句).从C11(N1570)6.7.3 / p6类型限定符(强调我的):

If an attempt is made to modify an object defined with a
const-qualifIEd type through use of an lvalue with non-const-qualifIEd
type,the behavior is undefined.

为了完整性,它可能值得添加,标准也说:

If an attempt is made to refer to an object defined with a
volatile-qualifIEd type through use of an lvalue with
non-volatile-qualifIEd type,the behavior is undefined.

因此后面的陈述,即:

*(int *)p = 16;

对于第二个短语也是未定义的(它是“双UB”).

我相信C的规则是相同的,但不要拥有C 14的副本来确认.

总结

以上是内存溢出为你收集整理的c – 可以将volatile volatile的间接变化视为未定义的行为吗?全部内容,希望文章能够帮你解决c – 可以将volatile volatile的间接变化视为未定义的行为吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1221402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存