2014-04-25 19:59
题目:C的关键字volatile有什么用?
解法:搞硬件设计的人好像更关注这个关键字。volatile本身是易变的意思,应该和persistent有反义词关系吧。说一个变量或是函数是易变的,就是说不知道会发生什么变化,所以不要编译器按规则进行优化处理,这就是我对此关键字唯一的了解了。目前为止,我貌似还没有遇到需要用到这个的情景,看来阅读过的代码还是太少。
代码:
1 // 13.5 Explain the "volatile" keyword in C. 2 // Answer: 3 // 1. disable compiler optimization on 'volatile' data. 4 // 2. the data declared as volatile won't be cached in register, because it might be modified in memory by other threads or subprocesses. 5 // 3. 'volatile' is more of an issue to be discussed in DSP design, thus paid less attention than other C keywords. 6 int main() 7 { 8 return 0; 9 }