为什么点对挥发性指针,就像"挥发性为int * P&QUOT ;,有用吗? [英] Why is a point-to-volatile pointer, like "volatile int * p", useful?

查看:108
本文介绍了为什么点对挥发性指针,就像"挥发性为int * P&QUOT ;,有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

挥发性是告诉编译器不优化的参考,让每一个读/写不使用存储在寄存器中的值,但不会真正的内存访问。我能理解这是一些普通的变量是有用的,但不知道如何挥发性影响的指针。

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

推荐答案

形式的指针

volatile int* p;

是一个指向 INT 编译器将视为挥发性。这意味着,编译器将假定它是可能的 P 在已经改变,即使没有什么源$ C ​​$ C表明指向变量这可能会发生。例如,如果设置了 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.

还有一个用例的挥发为int * :如果您声明 INT 挥发性,那么你不应该在它与普通的为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 指出是挥发性 ,所以它可能在寄存器中缓存 * p 的值不正确。事实上,在C ++中,上述code是一个错误。相反,你应该写

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 *p 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 挥发性INT点,所以不会(或不应该'T!)尝试优化访问,通过 * 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.

最后一个细节 - 你讨论的指针指向一个挥发性INT 。你也可以这样做:

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

int* volatile ptr;

这表示,指针的本身的是挥发性,这意味着编译器不应该尝试缓存指针在内存或尝试优化指针值,因为指针本身可能被别的东西(硬件,另一个线程等)可以结合在一起,这些重新分配得到,如果你想获得此兽:

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, another thread, 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.

希望这有助于!

这篇关于为什么点对挥发性指针,就像"挥发性为int * P&QUOT ;,有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆