关于关键字 volatile
关于 volatile 的使用,也是 C 语言面试的月经问题。标准答案来了:
volatile is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time-without any action being taken by the code the compiler finds nearby. The implications of this are quite serious.
翻译一下:volatile 是在声明变量时,使用的一个修饰符。这个修饰符是为了告诉编译器,这个变量虽然没有在上下文中引用,但是它的值可能随时会改变,所以,不要去对它进行优化动作!
使用场景,最多的,就是对硬件寄存器的操作;比如 IO 或者 ADC 等寄存器,他们的值会被外界的电平改变。
参考文章里介绍了下面三种使用场景:
- Memory-mapped peripheral registers
- Global variables modified by an interrupt service routine
- Global variables within a multi-threaded application
——————
无论在哪里做什么,只要坚持服务、创新、创造价值,其他的东西自然都会来的。
无论在哪里做什么,只要坚持服务、创新、创造价值,其他的东西自然都会来的。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步