我什么时候会在 C++ 中使用 const volatile、register volatile、static volatile?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16259939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When would I use const volatile, register volatile, static volatile in C++?
提问by Avraam Mavridis
I am wondering about the different uses of the volatilekeyword in combination with register, constand statickeywords. I am not sure what are the effects, so I think:
我想知道volatile关键字与register、const和static关键字的不同用途。我不确定有什么影响,所以我认为:
register volatile int T=10;
Suggest the compiler to store T in a register and the value of T can be modified from somewhere outside (OS, hardware, another thread)
建议编译器将 T 存储在寄存器中,并且可以从外部(操作系统、硬件、另一个线程)修改 T 的值
const volatile int T=10;
The program itself can not modify T, but T can be modified frow somewhere outside the code.
程序本身不能修改T,但可以在代码之外的某处修改T。
static volatile int T=10;
If T is a data member of a class it means that all the objects of the class have the same value for T and T can be modified from somewhere outside. If T is a global variable in a file, the source code in other files (that are part of the project) cannot access T, but T can be accessed from somewhere outside. If T is a local variable in a function,once it has been initialized remains in the memory until the end of the program and can be modified from somewhere outside.
如果 T 是类的数据成员,则意味着该类的所有对象都具有相同的 T 值,并且可以从外部修改 T。如果 T 是文件中的全局变量,则其他文件(属于项目的一部分)中的源代码无法访问 T,但可以从外部某处访问 T。如果 T 是一个函数中的局部变量,一旦它被初始化,它就会保留在内存中直到程序结束,并且可以从外部的某个地方进行修改。
Are my thoughts correct and can any experienced C++ developer give an example where the above maybe used in real-world applications or it is very rare?
我的想法是否正确,任何有经验的 C++ 开发人员都可以举一个例子,其中上述内容可能在现实世界的应用程序中使用还是非常罕见?
回答by Alok Save
register volatile int T=10;
volatile
qualifier means that the compiler cannot apply optimizations or reorder access to T
, While register
is a hint to the compiler that T
will be heavily used. If address of T
is taken, the hint is simply ignored by the compiler. Note that register
is deprecated but still used.
volatile
限定符意味着编译器不能对 应用优化或重新排序访问T
,Whileregister
是对T
将被大量使用的编译器的提示。如果T
取地址,编译器会简单地忽略该提示。请注意,register
已弃用但仍在使用。
Practical Usage:
实际用法:
I have never used it never felt the need for it and can't really think of any right now.
我从来没有用过它,从来没有觉得需要它,现在也想不出任何东西。
const volatile int T=10;
const
qualifier means that the T
cannot be modified through code. If you attempt to do so the compiler will provide a diagnostic. volatile
still means the same as in case 1. The compiler cannot optimize or reorder access to T
.
const
限定符意味着T
不能通过代码修改。如果您尝试这样做,编译器将提供诊断信息。volatile
仍然与情况 1 中的含义相同。编译器无法优化或重新排序对 的访问T
。
Practical Usage:
实际用法:
- Accessing shared memory in read-only mode.
- Accessing hardware registers in read-only mode.
- 以只读模式访问共享内存。
- 以只读模式访问硬件寄存器。
static volatile int T=10;
static
storage qualifier gives T
static storage duration (C++11 §3.7) and internal linkage, while volatile
still governs the optimization and reordering.
static
storage 限定符给出T
静态存储持续时间(C++11 §3.7)和内部链接,同时volatile
仍然控制优化和重新排序。
Practical Usage:
实际用法:
- Same as
volatile
except that you need the object to have static storage duration and to be inaccessible from other translation units.
- 一样
volatile
,除了你所需要的对象具有静态存储时间和从其他翻译单元无法访问。