这样的指向易失性的指针有用

这样的指向易失性的指针有用

本文介绍了为什么像“volatile int * p"这样的指向易失性的指针有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

volatile 是告诉编译器不要优化引用,这样每次读/写都不会使用寄存器中存储的值,而是进行真正的内存访问.我可以理解它对一些普通变量很有用,但不理解 volatile 如何影响指针.

volatile is to tell the compiler not to optimize the reference, so that every read/write does not use the value stored in register but does a real memory access. I can understand it is useful for some ordinary variable, but don't understand how volatile affects a pointer.

volatile int *p = some_addr;
int a = *p; // CPU always has to load the address, then does a memory access anyway, right?

如果声明为 int *p = some_addr 有什么区别?

What is the difference if it has been declared as int *p = some_addr?

推荐答案

表单指针

volatile int* p;

是一个指向 int 的指针,编译器会将其视为 volatile.这意味着编译器会假设 p 指向的变量可能已经改变,即使源代码中没有任何内容表明这可能发生.例如,如果我将 p 设置为指向一个常规整数,那么每次我读取或写入 *p 时,编译器都会意识到该值可能已意外更改.

is a pointer to an int that the compiler will treat as volatile. This means that the compiler will assume that it is possible for the variable that p is pointing at to have changed even if there is nothing in the source code to suggest that this might occur. For example, if I set p to point to a regular integer, then every time I read or write *p , the compiler is aware that the value may have changed unexpectedly.

volatile int* 还有一个用例:如果你将 int 声明为 volatile,那么你不应该指向它带有一个常规的 int*.例如,这是一个坏主意:

There is one more use case for a volatile int*: If you declare an int as volatile, then you should not point at it with a regular int*. For example, this is a bad idea:

volatile int myVolatileInt;
int* ptr = &myVolatileInt; // Bad idea!

这是因为C编译器不再记得ptr指向的变量是volatile,所以它可能缓存了*的值ptr 在寄存器中不正确.事实上,在C++中,上面的代码是错误的.相反,你应该写:

The reason for this is that the C compiler no longer remembers that the variable pointed at by ptr is volatile, so it might cache the value of *ptr in a register incorrectly. In fact, in C++, the above code is an error. Instead, you should write:

volatile int myVolatileInt;
volatile int* ptr = &myVolatileInt; // Much better!

现在,编译器记住 ptr 指向一个 volatile int,所以它不会(或不应该!)尝试通过 优化访问*ptr.

Now, the compiler remembers that ptr points at a volatile int, so it won't (or shouldn't!) try to optimize accesses through *ptr.

最后一个细节 - 您讨论的指针是指向 volatile int 的指针.你也可以这样做:

One last detail - the pointer you discussed is a pointer to a volatile int. You can also do this:

int* volatile ptr;

这表示指针本身volatile,这意味着编译器不应该尝试在内存中缓存指针或尝试优化指针值,因为指针本身可能会被其他东西(硬件等)重新分配.如果你想得到这个野兽,你可以将它们组合在一起:

This says that the pointer itself is volatile, which means that the compiler shouldn't try to cache the pointer in memory or try to optimize the pointer value because the pointer itself might get reassigned by something else (hardware, etc.) You can combine these together if you'd like to get this beast:

volatile int* volatile ptr;

这表示指针和被指点对象都可能被意外更改.编译器无法优化指针本身,也无法优化所指向的内容.

This says that both the pointer and the pointee could get changed unexpectedly.The compiler can't optimize the pointer itself, and it can't optimize what's being pointed at.

希望这有帮助!

这篇关于为什么像“volatile int * p"这样的指向易失性的指针有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:37