什么时候需要 volatile 指针? [英] When would you need a volatile pointer?
问题描述
我知道您何时需要指向易失性值的指针(例如,读取内存映射外设).但是你什么时候需要指针本身是可变的?>
I understand when you need a pointer to a volatile value (e.g. doing a read of a memory mapped peripheral). But when would you need the pointer itself to be volatile?>
推荐答案
为了详细说明我的评论,首先要记住 volatile
的含义很重要:它是给编译器的信号声明的变量可以通过编译器无法看到或控制的机制以意想不到的方式更改,并且读取和写入可能会产生副作用.因此,编译器通常每次都会从主内存重新加载变量的值,而不是使用缓存的值,并且不会执行移除、重新排序或省略对此类 volatile
变量的读取或写入的优化.
To elaborate on my comment a little bit, it's important to first keep in mind what volatile
means: it's a signal to the compiler that the variable being declared can change in unexpected ways through mechanisms that the compiler cannot see or control and that reads and writes may have side-effects. As a result, a compiler will typically reload the value of the variable every time from main memory instead of using a cached value and will not perform optimizations that remove, reorder or elide reads or writes to such volatile
variables.
通常这种事情是大多数用户空间代码不必担心的,并且volatile
的合法使用很少.
Typically this sort of thing is something that most userspace code doesn't have to worry about and legitimate uses of volatile
from there are few and far between.
您的问题还引用了该语言的一个微妙且鲜为人知的特性:能够将 指针 本身声明为 volatile
(或 const
) 而不是它指向的东西.您可以使用以下语法执行此操作:
Your question also references a subtle and not well-known feature of the language: the ability to declare a pointer itself as volatile
(or const
) instead of thing that it points to. You do this with this syntax:
int *volatile p = 0x74;
这声明了一个 volatile
指针,该指针指向内存位置 0x74
处的 int
.对比一下:
This declares a volatile
pointer that points to an int
at memory location 0x74
. Contrast this with:
volatile int *p = 0x74;
它声明了一个指向 volatile int
地址0x74
which declares a pointer to a volatile int
at address 0x74
这两者的区别在于当您访问p
时会发生什么.我们只关心读取案例:
The difference between this two is what happens when you access p
. We'll only concern ourselves with the read case:
如果每次读取访问都指向 volatile int
的指针,编译器将转到内存位置 0x74
并读取(可能使用特殊的 CPU 指令)int
值在该位置 - code 不会缓存该值(但硬件可能会缓存).
In the case of the pointer to volatile int
for every read access the compiler will go to memory location 0x74
and read (perhaps using special CPU instructions) the int
value at that location - the value will not be cached by the code (but it might be by the hardware).
在 volatile
指向 int
的指针的情况下,编译器将在每次访问时重新加载指针的 value 然后去读取该地址的 int
.该地址不会被缓存在代码中(但同样,它可能会被硬件缓存).
In the case of the volatile
pointer to int
the compiler will reload the value of the pointer at every access and then go and read the int
at that address. The address will not be cached in the code (but again, it may be cached by the hardware).
与写入类似.
同样,请务必记住 volatile
限制编译器(例如,它不能优化写入或读取),因为对 volatile
变量的任何操作都可能具有编译器不可见的副作用.这种副作用的一个例子可能是产生中断、使 LED 闪烁、写入审计记录或更改寄存器的值.
Again, be sure to remember that volatile
restricts the compiler (e.g. it can't optimize away a write or a read) since any operation on the volatile
variable might have side-effects that are not visible to the compiler. An example of such a side effect might be generating an interrupt, blinking an LED, writing an audit record or changing the value of a register.
这篇关于什么时候需要 volatile 指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!