volatile int i=10;
int a = i;
int b = i; volatile 指出 i 是随时可能发生变化的,每次使用它的时候必须从 i的地址中读取,因而编译器生成的汇编代码会重新从i的地址读取数据放在 b 中。而优化做法是,由于编译器发现两次从 i读数据的代码之间的代码没有对 i 进行过操作,它会自动把上次读的数据放在 b 中。而不是重新从 i 里面读。这样以来,如果 i是一个寄存器变量或者表示一个端口数据就容易出错,所以说 volatile 可以保证对特殊地址的稳定访问。
Fundamental types are the arithmetic types, void, and std::nullptr_t.
Compound types are arrays, functions, pointers, references, classes, unions, enumerations, and pointers to non-static members.
A cv-unqualified type is any of those types.
For any cv-unqualified type, there are three corresponding cv-qualified types:
- const-qualified - with the
constcv-qualifier - volatile-qualified - with the
volatilecv-qualifier - const-volatile-qualified - with both the
constandvolatilecv-qualifiers
Ref :
https://zhuanlan.zhihu.com/p/62060524#:~:text=volatile%20%E5%85%B3%E9%94%AE%E5%AD%97%E6%98%AF%E4%B8%80,%E7%89%B9%E6%AE%8A%E5%9C%B0%E5%9D%80%E7%9A%84%E7%A8%B3%E5%AE%9A%E8%AE%BF%E9%97%AE%E3%80%82
No comments:
Post a Comment