简体   繁体   English

c中的volatile和指针变量之间的区别

[英]Difference between a volatile and a pointer variable in c

volatile和指针变量都从地址获取值而不是优化,因此明显的不同。

volatile is a storage class, along with register , static , external . volatile是一个存储类,还有registerstaticexternal volatile says that the value of a volatile variable could be changed by other forces besides the running program, so the compiler must be careful to not optimize fetching a fresh copy of the variable with each use. volatile表示volatile变量的值可以通过正在运行的程序以外的其他力来更改,因此编译器必须小心,不要在每次使用时优化获取变量的新副本。

A pointer contains the address of a memory location. 指针包含一个存储单元的地址。 To access what it points at, it must be dereferenced. 要访问它所指向的内容,必须将其取消引用。

Volatile is an indication to the compiler to re-fetch the value from the memory location than use the value stored in registers. 易失性是指示编译器比使用寄存器中存储的值重新从存储器位置取值。 This is because the memory location may have been updated (eg by other threads). 这是因为内存位置可能已更新(例如,通过其他线程)。 That is the meaning of 那就是

fetch the value from the address 从地址获取值

not act as a pointer. 不充当指针。 You can also volatile a non-pointer variable eg primitive. 您也可以使非指针变量(例如,原始变量)易失。
So an int variable is refetched and not use the stored value in registers. 因此,将重新获取一个int变量,而不使用寄存器中存储的值。
It also enforces certain semantics concerning read/write (but this is not related to your OP) 它还会强制执行与读/写有关的某些语义(但这与您的OP不相关)

Volatile is a storage class which tells the compiler to fetch the value from memory every time it is accessed and write to it every time it is written. Volatile是一种存储类,它告诉编译器每次访问时从内存中获取值,并在每次写入时将其写入。 It is usually used when some entity other than your program may also change the value at a certain address. 通常在程序以外的某些实体也可能更改某个地址的值时使用。

Compilers optimize the programs in many ways. 编译器以多种方式优化程序。 For example if you have following code: 例如,如果您有以下代码:

int *ptr=0x12345678;
int a;
*ptr=0x10;
a=*ptr;

then the compiler may optimize the statement a=*ptr; 然后编译器可以优化语句a=*ptr; to a=0x10; a=0x10; to avoid memory access. 以避免内存访问。 the justification is that because you just wrote 0x10; 理由是因为您只写了0x10; to *ptr so when you read *ptr you are going to get 0x10. 到* ptr,因此当您阅读* ptr时,您将得到0x10。

In normal cases this assumption is true but consider the case where the address 0x12345678 is the address of some memory mapped register of an embedded system's UART and writing 0x10 to it tells it to read a character from console attached. 在正常情况下,此假设是正确的,但请考虑以下情况:地址0x12345678是嵌入式系统UART的某些内存映射寄存器的地址,向其写入0x10会告诉它从连接的控制台读取字符。 The character that is read is then written back to address 0x12345678 so the user can get it from there. 然后将读取的字符写回到地址0x12345678,以便用户从那里获取。 Now in this case the above optimization will cause problems. 现在,在这种情况下,以上优化将导致问题。 So you want something to tell the compiler to to read/write the value to pointer every time it is accessed and don't optimize accesses to it. 因此,您需要让编译器在每次访问指针时都读取/写入指向该指针的值,而不是优化对其的访问。 So you declare the pointer ptr volatile telling the compiler not to optimize accesses to it. 因此,您将指针ptr声明为volatile,告诉编译器不要优化对其的访问。

volatile int *ptr=0x12345678;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM