Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of volatile keyword in C language

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "detailed introduction of volatile keywords in C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction of the volatile keyword in C language.

Volatile keyword in C language

The volatile keyword is a very unpopular keyword in the C language because there are not many scenarios in which it is used.

When not using this keyword, CPU may make some optimizations to our code:

The data in memory is put into CPU for operation or control, and the value of this data is put into the register, and then the data in the register is operated or controlled, for an endless loop int flag=1;while (flag); if optimized, the next cycle does not need to put the value in flag memory into the register again, but directly uses the values already in the register to loop. If you don't optimize, next time you need to put the value in flag memory into a register, and then use the data in the register.

To sum up, when you encounter a variable declared by this keyword, the compiler no longer optimizes the code that accesses the variable, providing stable access to its address; if you do not use valatile, the compiler optimizes the declared statement.

There is not much difference between the two cases in the single-threaded case, but in the multithreaded case, there may be other logic to change the flag to 0, and if optimized, the dead loop will not stop.

So the role of volatile is to make variables not to be optimized by CPU, to achieve stable access to memory.

For example, the following code:

We use the command gcc test.c-O2-g under gcc to optimize the code, then use the command objdump-S-d a.out > a.s to put the optimized assembly code into the a.s file, and then use vim a.s to view the a.s file:

The program loops in this code all the time:

After joining volatile:

Then use the same command to view the a.s file:

You can see that each loop reads pass's data.

Conclusion: volatile ignores compiler optimization and maintains memory visibility.

In addition, const and volatile do not conflict:

Const volatile int a = 10

The const keyword requires that the variable a cannot be written directly, while the volatile keyword requires that every time the data is read, it is read from the memory where an is located, and does not change the value of the variable a.

At this point, I believe you have a deeper understanding of the "detailed introduction of volatile keywords in C language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report