您的位置:首页 > 编程语言 > C语言/C++

《Cracking the Coding Interview》——第13章:C和C++——题目5

2014-04-25 20:07 831 查看
2014-04-25 19:59

题目:C的关键字volatile有什么用?

解法:搞硬件设计的人好像更关注这个关键字。volatile本身是易变的意思,应该和persistent有反义词关系吧。说一个变量或是函数是易变的,就是说不知道会发生什么变化,所以不要编译器按规则进行优化处理,这就是我对此关键字唯一的了解了。目前为止,我貌似还没有遇到需要用到这个的情景,看来阅读过的代码还是太少。

代码:

// 13.5 Explain the "volatile" keyword in C.
// Answer:
//    1. disable compiler optimization on 'volatile' data.
//     2. the data declared as volatile won't be cached in register, because it might be modified in memory by other threads or subprocesses.
//    3. 'volatile' is more of an issue to be discussed in DSP design, thus paid less attention than other C keywords.
int main()
{
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐