C语言 了解 C 中的 volatile 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20027238/
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
Understanding volatile variables in C
提问by Sri Harsha Chilakapati
I'm currently learning C in class and I'm having some confusion understanding volatile variables. My textbook defines them as this.
我目前正在课堂上学习 C,但我对 volatile 变量的理解有些混乱。我的教科书将它们定义为这样。
Volatile Variables
易失性变量
The volatile variables are those variables that are changed at any time by other external program or the same program. The syntax is as follows.
volatile int d;
易失性变量是那些随时被其他外部程序或同一程序改变的变量。语法如下。
volatile int d;
What is exactly the difference between the normal variables and volatile variables? If volatile variables can be changed by external programs, how can I change the value from another external program.
正常变量和易失性变量之间究竟有什么区别?如果外部程序可以更改 volatile 变量,我该如何更改另一个外部程序的值。
Thanks.
谢谢。
回答by Shubham
volatilekeyword tells the compiler that the variable can be changed (sounds senseless) so you should take care while optimizing. For example consider this-
volatile关键字告诉编译器变量可以改变(听起来毫无意义)所以你在优化时应该小心。例如考虑这个 -
bool running = true;
while(running) {
//do something
}
The compiler may change while(running)to while(1)but if the variable runningis being affected by the code outside the while loop, like in multi-threading, this will create a bug very hard to spot. So the correct thing would be to declare runningas volatile.
编译器可能会更改while(running)为,while(1)但是如果变量running受到 while 循环外的代码的影响,就像在多线程中一样,这将产生一个很难发现的错误。所以正确的做法是声明running为volatile.
volatile bool running = true;
while(running) {
//do something
}
回答by Potatoswatter
volatileindicates that the bytes used to store an object may be changed or accessed by something else running in parallel with the program. In practice this is usually a hardware interface, but sometimes it is used to interface with the operating system as well.
volatile表示用于存储对象的字节可能会被与程序并行运行的其他东西更改或访问。在实践中,这通常是一个硬件接口,但有时也用于与操作系统接口。
Whereas constindicates that memory is read-only (to your program), volatileindicates that some other task has write access to it. const volatiletogether indicate that an object represents a hardwired input; you might see such a thing in a microcontroller with a memory-mapped sensor.
而const表示内存是只读的(对您的程序而言),则volatile表示其他一些任务对其具有写访问权限。const volatile一起表示一个对象代表一个硬连线输入;您可能会在带有内存映射传感器的微控制器中看到这样的事情。
How this affects your program is that the compiler treats accesses to the memory specially. If you access it twice, the compiler will not cache the first access and give you the same value twice. It will go to the memory hardware and perform two read operations. And when you modify the object, that modification is written immediately and exactly as you specify, with no buffering or reordering optimizations.
这如何影响您的程序是编译器专门处理对内存的访问。如果您访问它两次,编译器将不会缓存第一次访问并两次为您提供相同的值。它将转到内存硬件并执行两次读取操作。当您修改对象时,该修改会立即按照您指定的方式写入,无需缓冲或重新排序优化。

