使用 volatile 关键字修改 const 变量 [英] Modifying a const variable with the volatile keyword

查看:54
本文介绍了使用 volatile 关键字修改 const 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在回答 问题 并制作了这个测试程序.

I was answering a question and made this test program.

#include <stdio.h>
int main()
{
    volatile const int v = 5;
    int * a = &v;
    *a =4;
    printf("%d
", v);
    return 0;
}

如果没有 volatile 关键字,代码会优化(使用 -O3 apple clang 4.2 编译)var 的变化,它按预期工作并且 const 变量被正确修改.

Without the volatile keyword the code optimizes (compiled with -O3 apple clang 4.2) the change of the var away, with it works as expected and the const variable is modified correctly.

我想知道一个更有经验的 C 开发人员是否知道标准的一部分是否表明这是不安全的或 UB.

I was wondering if a more experienced C developer knows if there is a part of the standard that says this is unsafe or UB.

更新: @EricPostpischil 给了我这个标准引用

UPDATE: @EricPostpischil gave me this standards quote

根据 C 2011 (N1570) 6.7.3 6,程序不得修改其自身使用 const 限定类型定义的对象:如果尝试通过使用具有非 const 限定类型的左值,行为未定义."外部代理可以修改具有 volatile 限定类型的对象,根据 6.7.3 7:具有 volatile 限定类型的对象可能会以实现未知的方式进行修改或具有其他未知的副作用

A program may not modify its own object defined with a const-qualified type, per C 2011 (N1570) 6.7.3 6: "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." An external agent may modify an object that has volatile-qualified type, per 6.7.3 7: "An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects

我的程序违反了第一条规则,但我认为第二条规则可能会使程序免于第一条规则.

My program breaks the first rule but I thought that the second rule may exempt a program from the first.

更新 2:

具有 volatile 限定类型的对象可能会以实现未知的方式被修改或具有其他未知的副作用.因此,任何引用此类对象的表达式都应严格按照抽象机的规则进行评估,如 5.1.2.3 中所述.此外,在每个序列点,最后存储在对象中的值应与抽象机规定的值一致,除非被前面提到的未知因素修改.134) 构成对具有 volatile 限定类型的对象的访问是实现-定义.

An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Therefore any expression referring to such an object shall be evaluated strictly according to the rules of the abstract machine, as described in 5.1.2.3. Furthermore, at every sequence point the value last stored in the object shall agree with that prescribed by the abstract machine, except as modified by the unknown factors mentioned previously.134) What constitutes an access to an object that has volatile-qualified type is implementation-defined.

如果您查看此引用,您会看到 var 必须根据某些规则进行评估,我还没有通读 5.1.2.3 部分的所有内容,但我相信这可能会有所启发关于这个问题.

If you look at this quote you can see the var must be evaluated according to certain rules, I haven't read through all of section 5.1.2.3 but I believe that this may shed some light on the issue.

推荐答案

这是不安全的,因为不能保证在其他编译器中使用相同的行为.因此,您的代码依赖于编译器,甚至可能依赖于编译器开关.这就是为什么这是一个坏主意.

It is unsafe because the same behavior cannot be guaranteed for use in other compilers. So your code is compiler-dependent and may even be compiler switch dependent. That's why it's a bad idea.

这篇关于使用 volatile 关键字修改 const 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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