当前位置:网站首页>Volatile keyword
Volatile keyword
2022-07-06 22:31:00 【It's Beichen not too PI acridine】
volatile keyword
volatile Key words and const It is also a type modifier , The variable modified by it indicates that it can be changed by some unknown factors of the compiler , Like the operating system 、 Hardware or other threads . Encountered the variable declared by this keyword , The compiler no longer optimizes the code that accesses the variable , It can provide stable access to special addresses .
Let's take a look at the following example :
int i=10;
int j = i; //(1) sentence
int k = i; //(2) sentence
At this time, the compiler optimizes the code , Because in (1)、(2) In two statements ,i Not used as an lvalue . At this time, the compiler thinks i The value of has not changed , So in (1) Statement is fetched from memory i The value is assigned to j after , This value has not been lost , But in (2) Continue to use this value to k assignment . The compiler will not generate assembly code and fetch it from memory again i Value , This improves efficiency . But should pay attention to :(1)、(2) Between sentences i Not used as an lvalue .
Another example :
volatile int i=10;
int j = i; //(3) sentence
int k = i; //(4) sentence
volatile The keyword tells the compiler i It can change at any time , Every time you use it, you must take it out of memory i Value , Therefore, the assembly code generated by the compiler will be restarted from i Read the data at the address of and put it in k in .
So it looks like , If i It is a register variable or represents a port data or shared data of multiple threads , It's easy to make mistakes , So volatile It can guarantee stable access to special addresses .
const volatile int i=10; Is there a problem with this line of code ? without , that i What is the attribute ?
Answer one : No problem , For example, read-only status registers . It is volatile, Because it can be changed unexpectedly ; It is const, Because the program should not try to modify it .volatile and const Not contradictory , But the scope of control is different , One is outside the program itself , The other is the program itself .
Answer two : That's all right. ,const and volatile These two type qualifiers are not contradictory .const Express ( Runtime ) Constant semantics : By const The modified object cannot be modified in its scope , The compiler is not interested in trying to modify directly const The expression of the object will cause compilation errors .volatile Express “ Changeable ”, That is, at run time, the object may be modified outside the control flow of the current program context ( For example, it is modified by other threads in multithreading ; The memory of the object may be randomly modified by multiple hardware devices ): By volatile Decorated object , The compiler will not optimize the operation of this object . An object can be simultaneously const and volatile modification , Indicates that this object embodies constant semantics , But at the same time, it may be modified unexpectedly by the program context of the current object .
边栏推荐
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- Export MySQL table data in pure mode
- Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
- (十八)LCD1602实验
- NetXpert XG2帮您解决“布线安装与维护”难题
- Applet system update prompt, and force the applet to restart and use the new version
- Chapter 3: detailed explanation of class loading process (class life cycle)
- Aardio - 通过变量名将变量值整合到一串文本中
- [Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
- 第3章:类的加载过程(类的生命周期)详解
猜你喜欢

Signed and unsigned keywords

Chapter 4: talk about class loader again

ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv

Config:invalid signature solution and troubleshooting details

UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop

rust知识思维导图xmind

Should novice programmers memorize code?

Attack and defense world ditf Misc

2022-07-04 mysql的高性能数据库引擎stonedb在centos7.9编译及运行
![[linear algebra] determinant of order 1.3 n](/img/6e/54f3a994fc4c2c10c1036bee6715e8.gif)
[linear algebra] determinant of order 1.3 n
随机推荐
[leetcode] 19. Delete the penultimate node of the linked list
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
Jafka来源分析——Processor
SQL Server生成自增序号
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
UE4蓝图学习篇(四)--流程控制ForLoop和WhileLoop
第4章:再谈类的加载器
MySQL教程的天花板,收藏好,慢慢看
中国固态氧化物燃料电池技术进展与发展前景报告(2022版)
中国VOCs催化剂行业研究与投资战略报告(2022版)
Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)
Management background --5, sub classification
LeetCode 练习——剑指 Offer 26. 树的子结构
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
【LeetCode】19、 删除链表的倒数第 N 个结点
uniapp滑动到一定的高度后固定某个元素到顶部效果demo(整理)
枚举与#define 宏的区别
config:invalid signature 解决办法和问题排查详解
(18) LCD1602 experiment
GD32F4XX串口接收中断和闲时中断配置