当前位置:网站首页>Meaning and usage of volatile
Meaning and usage of volatile
2022-04-23 15:55:00 【Sola_ Ex】
volatile The meaning and usage of
Preface
I was asked a few days ago volatile This problem , To tell you the truth, I'm right volatile In fact, it is only limited to the level of preventing from being optimized by the compiler , But I don't know what kind of situation it actually needs to be applied to , So yesterday volatile I studied again , Also decided to write it down and record it .
volatile The definition of
volatile Is a feature modifier , It is a keyword on the compiler , Just follow static、const equally . Its ultimate goal is to prevent the instruction to be executed from being optimized by the compiler .volatile The variable is that it can be changed unexpectedly , such , The compiler will not assume the value of this variable .
effect
It doesn't work very miraculously , Take an example ,
EX1:
In a program like this :
AX_1 = 0;
AX_1 = 1;
AX_1 = 2;
AX_1 = 3;
AX_1 = 4;
No idea , Last AX_1 The value of is 4, But if I ask you , In this code ,AX_1 Have you ever experienced from 0-4 The change of ? The answer is not necessarily , Why? ? Because in different compiler environments , The optimization level of code is different , Even some can be set to no optimization effect . Use Keil As an example ,
Many optimization levels will directly default to -O3 Level of , The higher the optimization level , The size of the resulting firmware may also be smaller , But when programming , I don't pay attention to many details , Will be directly optimized by the compiler . Of course , Under such conditions ,AX_1 It's impossible, manager 0-4 Changing , It will be omitted directly 0-3 The change of , It arrived directly 4. In fact, I also have a question here , hypothesis AX_1 It's a status register ? Of course, it is not common to assign values to registers in this way .
EX2:
Emergencies , Such as interruption .
Actually from volatile The italics in the definition of ,“ This variable may be changed unexpectedly ”, Interruption is doing something like this ? For interrupts , Events can be executed periodically , It can also be executed cyclically , But it's all a sudden treatment , If you suddenly enter the interrupt , Then modify a value , It's actually an unexpected change , Sudden . So for the variables that need to be modified , Use volatile To modify , This problem can be well avoided , Of course, reducing the compiler optimization level may also be achieved , However, such areas that need to be optimized by the compiler have not been optimized , It's a waste of memory , So it's a good thing to write more standardized .
版权声明
本文为[Sola_ Ex]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231554163833.html
边栏推荐
- leetcode-374 猜数字大小
- 北京某信护网蓝队面试题目
- API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
- One brush 312 - simple repetition set - Sword finger offer 03 Duplicate number in array (E)
- MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
- Single architecture system re architecture
- PHP function
- Neodynamic Barcode Professional for WPF V11. 0
- One brush 314 sword finger offer 09 Implement queue (E) with two stacks
- Timing model: gated cyclic unit network (Gru)
猜你喜欢
New developments: new trends in cooperation between smartmesh and meshbox
R语言中实现作图对象排列的函数总结
负载均衡器
IronPDF for .NET 2022.4.5455
Why disable foreign key constraints
Distinct use of spark operator
Application of Bloom filter in 100 million flow e-commerce system
Spark 算子之partitionBy
MySQL - MySQL查询语句的执行过程
CAP定理
随机推荐
时序模型:长短期记忆网络(LSTM)
一刷312-简单重复set-剑指 Offer 03. 数组中重复的数字(e)
Tencent offer has been taken. Don't miss the 99 algorithm high-frequency interview questions. 80% of them are lost in the algorithm
现在做自媒体能赚钱吗?看完这篇文章你就明白了
一刷313-剑指 Offer 06. 从尾到头打印链表(e)
Open source project recommendation: 3D point cloud processing software paraview, based on QT and VTK
MySQL集群模式与应用场景
大型互联网为什么禁止ip直连
utils. Deprecated in35 may be cancelled due to upgrade. What should I do
Ice -- source code analysis
leetcode-396 旋转函数
布隆过滤器在亿级流量电商系统的应用
s16.基于镜像仓库一键安装containerd脚本
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
捡起MATLAB的第(10)天
Spark 算子之partitionBy
捡起MATLAB的第(9)天
pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
Master vscode remote GDB debugging
捡起MATLAB的第(7)天