关键字volatile有什么含义
(2015-10-06 16:32:55)
标签:
cvolatile关键字 |
分类: Offer之路 |
引子1
|
1
2
3
4
|
void Delay(UINT32 n){ while(—n);} |
|
1
2
3
4
|
void Delay(volatile UINT32 n){ while(—n);} |
引子2
|
1
2
3
4
|
int square(volatile int *ptr){ return
*ptr * *ptr;} |
|
1
2
3
4
5
6
7
|
int square(volatile int *ptr) { int
a,b; a
= *ptr; b
= *ptr; return
a * b;} |
|
1
2
3
4
5
6
|
int square(volatile int *ptr) { int
a; a
= *ptr; return
a * a;} |
正文
|
|
int volatile nVint;//当要求使用volatile |
|
1
2
3
4
|
volatile int i =
10;int a =
i;... //其他代码,并未明确告诉编译器,对i进行过操作int b =
i; |
- 中断服务程序中修改的供其它程序检测的变量需要加volatile;
- 多任务环境下各任务间共享的标志应该加volatile;
- 存储器映射的硬件寄存器通常也要加volatile说明,因为每次对它的读写都可能由不同意义。

加载中…