C语言 C语言中的volatile关键字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5822386/
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
the volatile keyword in C language?
提问by WindsurferOak
I have a question about volatile in C language.
我有一个关于 C 语言中 volatile 的问题。
I read some tutorial but still can not figure out, some says the volatile tells the complier optimizer that operations involving this variable should not be optimized in certain ways. this means anytime the value of a variable is change in register, then the value should affect the memory.
我阅读了一些教程,但仍然无法弄清楚,有人说 volatile 告诉编译器优化器,不应以某些方式优化涉及此变量的操作。这意味着只要变量的值在寄存器中发生变化,该值就会影响内存。
And also some say that volatile mean that the value can be changed by means outside this code.
还有人说 volatile 意味着可以通过此代码之外的方式更改该值。
I can not understand the second saying, so the volatile variable can be changed by means outside of this code? how? and are these two says both right?
我无法理解第二句话,所以可以通过此代码之外的方式更改 volatile 变量?如何?这两个说的都对吗?
回答by WindsurferOak
The statement "the value can be changed by means outside of this code" basically means that another program or hardware can update that variable. This is totally possible. One way of thinking of this is by relating this concept to a file that is shared among multiple programs. A file can be opened, written, and read by many programs at once. When you read from a file you want to make sure that you are reading the latest update and not the oldest.
声明“可以通过此代码之外的方式更改值”基本上意味着另一个程序或硬件可以更新该变量。这是完全可能的。一种思考方式是将此概念与在多个程序之间共享的文件相关联。一个文件可以被多个程序同时打开、写入和读取。当您从文件中读取时,您希望确保您正在读取最新的更新而不是最旧的。
Going back to the volatile keyword, placing volatile before a variable, in effect, does the same thing. It makes sure that what you are reading out of the variable isn't based on the compiler's optimization or an old copy of the variable that your program had. Moreover, the volatile keyword ensures that the variable is fetched from memory on every access. Therefore, both of those statements are correct regarding the volatile keyword.
回到 volatile 关键字,将 volatile 放在变量之前,实际上做同样的事情。它确保您从变量中读取的内容不是基于编译器的优化或您的程序拥有的变量的旧副本。此外, volatile 关键字确保每次访问时都从内存中获取变量。因此,关于 volatile 关键字,这两种说法都是正确的。
回答by aviraldg
C isn't necessarily for computers. For example, if you're developing for the Game Boy Advance, you often come upon memory locations that are modified by the hardware, so you might not modify the variablein your code, but it gets modified anyway. That's what volatilemeans.
C 不一定适用于计算机。例如,如果您正在为 Game Boy Advance 进行开发,您经常会遇到由硬件修改的内存位置,因此您可能不会修改代码中的变量,但无论如何它都会被修改。就是这个volatile意思。
By adding the volatilekeyword, you're telling the compiler that "The value stored in this variable (memory location) might change without mycode doing anything."
通过添加volatile关键字,您告诉编译器“存储在此变量(内存位置)中的值可能会更改,而我的代码无需执行任何操作。”
回答by Oliver Charlesworth
Consider any of the following:
考虑以下任何一项:
- a multi-threaded application,
- an application using shared memory,
- an application running on a platform that maps I/O registers into the address space,
- an application with hardware DMA occurring in the background.
- 一个多线程应用程序,
- 使用共享内存的应用程序,
- 在将 I/O 寄存器映射到地址空间的平台上运行的应用程序,
- 在后台发生硬件 DMA 的应用程序。
In each of these situations, memory can be altered outside the current thread.
在每种情况下,都可以在当前线程之外更改内存。
Note that "anytime the value of a variable is change in register, then the value should affect the memory" is correct, just not very clear.
请注意,“只要变量的值在寄存器中发生变化,那么该值就会影响内存”是正确的,只是不太清楚。
回答by Oliver Charlesworth
A memory location can be changed outside a programs code in a huge number of ways. For example, a DMA read from a disk can write into a buffer, or a memory mapped device can change a location because of some event on the device.
可以通过多种方式在程序代码之外更改内存位置。例如,从磁盘读取的 DMA 可以写入缓冲区,或者内存映射设备可以由于设备上的某些事件而更改位置。
回答by Flinsch
this addresses, for example, multi-threading applications: The value of a variable can be changed by several threads and thus has to be "synchronized" with the memory on each access (regardless of whether to read or write the value).
例如,这解决了多线程应用程序:变量的值可以由多个线程更改,因此必须在每次访问时与内存“同步”(无论是读取还是写入值)。
回答by user2212525
Declaring a volatile variable means, you are instructing compiler not to optimize the code snippet around that variable. This is to force the cpu to NOT to use the variable value from local registers or cache memory but to fetch the value from main memory every time.
声明一个 volatile 变量意味着,您正在指示编译器不要优化围绕该变量的代码片段。这是为了强制 cpu 不使用本地寄存器或缓存中的变量值,而是每次从主内存中获取值。

