当前位置:网站首页>Volatile of C language
Volatile of C language
2022-07-05 07:56:00 【feiyingzaishi】
C Language volatile
emOsprey Osprey talk about MCU 2 month 21 Japan
Estimated reading time : 4 minute
and const Different ( About const You can see const Section ), When a variable is declared as volatile, It means that this variable will be changed unexpectedly . The most typical is I/O The input register of , Because the value of this variable is related to the external level , Once the external level changes , Then this variable will change . Of course, there are other registers as well , For example, various status registers 、 Timing counter, etc , Their change depends on hardware , Your program can only read data , So it must be stated as volatile Talent , So when your optimization level improves , Your program can also ensure that there will be no problems due to over optimization .
Then state as volatile What effect does the variable of have on the compiler ? We know that the compiler has optimization function , A lot of times , The value of some variables may be unchanged during operation , If you need to get the variable value from the original memory every time you access this variable , That's a waste of time , If you let your compiler not optimize , Then every time it accesses this variable, it will read data from memory , This is not only inefficient , The amount of code will also be relatively large , Once optimized , The compiler will save the amount it considers unchanged in the internal register , You can access this register every time you access this variable , In this way, the operation efficiency will be greatly improved . So there are usually two versions when writing code , One is Debug edition , One is Release edition . Debug Version and Release One of the differences between versions is the optimization level , Of course, their differences are not only reflected in these two aspects .
First, let's take a look at the code under two different optimization levels :

You can see that the effect is still very obvious .
First of all Code Less 24.1%,RO-data unchanged ,RW-data Reduced 8, This is because I stated in the program that two pointers are not used in the program , So it was optimized .

It is these two goods that occupy 8 Byte space , Now because the optimization level is improved , It's been optimized . then ZI-data remain unchanged .
So it looks like , The optimization effect is still obvious . But will there be any problems with the operation of the code , It's largely volatile The problem. .
Now look at the library function GPIO Structure declaration :

You can see that each register declaration is __IO, and __IO Finally, you can see volatile:

So every time the compiler encounters a declaration of volatile The variables in the register cannot be used for backup , Instead, access data from the original memory .
In addition to hardware registers , There are also variables shared by multiple threads and variables used by interrupt service programs , They are all similar , It may not change in one function , And in other functions ( Interrupt handler functions or functions of other threads ) Possible changes , If you don't declare volatile, Then the compiler sees that this variable does not change in a function , You may only access memory once , Then save to register , When used, you may only use copies of registers , Once other programs also write to this variable , It is imperceptible , It may also destroy this write operation , See the semaphore section for details .
Speaking of this , Just think of the situation encountered by a friend . At that time, he had a variable added by himself in the interrupt handler , And in the main Function , Then debugging found that this variable was always read as 0, There's no change at all , But you can be sure that you have entered the interrupt handler . At that time, I also knew such a situation , But I have other things , There is no deep study . Later, he adjusted it for a long time and added volatile Keywords can be solved , Then I asked him if the compilation optimization level was too high , He said that the optimization level had never been changed , So he just suspects , I didn't really look at the level of his compiler . When I asked him to verify again, I found that the optimization level was really high , Not at all level 0, It is level 3.
Yes, of course , If you know a little assembly language , This problem should be easier to solve , Because no matter how optimized the compiler , Will eventually be reflected at the assembly level , If you find that assembly code always uses a copy of the register , Then we can judge that the optimization is basically too high , At this time, set the optimization level to level 0 There is no problem , But the best way is to add volatile, Because you may optimize it in the future !
-THE END-
边栏推荐
- Makefile application
- .NET服务治理之限流中间件-FireflySoft.RateLimit
- Significance and requirements of semiconductor particle control
- Consul安装
- Explain STM32 startup file in detail
- Distinction between heap and stack
- A simple method to prove 1/t Fourier transform
- About yolov3, conduct map test directly
- Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
- Win10 shortcut key
猜你喜欢

Practical application cases of digital Twins - fans

Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world

Use of orbbec Astra depth camera of OBI Zhongguang in ROS melody
![C WinForm [get file path -- traverse folder pictures] - practical exercise 6](/img/8b/1e470de4e4ecd4fd1bb8e5cf23f466.jpg)
C WinForm [get file path -- traverse folder pictures] - practical exercise 6

Significance and requirements of semiconductor particle control

Cadence simulation encountered "input.scs": can not open input file change path problem

Ads learning record (lna_atf54143)

Altium Designer 19.1.18 - 隐藏某一个网络的飞线

Embedded AI intelligent technology liquid particle counter
![[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen](/img/43/7f8becc09c5ce7fe401bed140608f3.jpg)
[untitled] record the visual shock of the Winter Olympics and the introduction of the display screen
随机推荐
Global and Chinese market of quenching furnaces 2022-2028: Research Report on technology, participants, trends, market size and share
Oracle triggers and packages
Global and Chinese markets for waste treatment air switches 2022-2028: Research Report on technology, participants, trends, market size and share
Halcon's practice based on shape template matching [1]
Record the visual shock of the Winter Olympics and the introduction of the screen 2
Summary of STM32 serial port sending and receiving data methods
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
How to realize audit trail in particle counter software
Define in and define out
Some tips for using source insight (solve the problem of selecting all)
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Query the table name used by kettle in Oracle
研究发现,跨境电商客服系统都有这五点功能!
Can't find real-time chat software? Recommend to you what e-commerce enterprises are using!
万字详解八大排序 必读(代码+动图演示)
Record the opening ceremony of Beijing Winter Olympics with display equipment
String judgment
C language uses arrays to realize the intersection, union, difference and complement of sets
Network port usage
Day09 how to create packages import package naming conventions Alibaba Development Manual