In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what is the use of volatile in C++". In daily operation, I believe many people have doubts about the use of volatile in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is the use of volatile in C++?" Next, please follow the editor to study!
1. Why use volatile?
The volatile keyword in const + corresponds to a const and is used to modify variables and is usually used to establish a language-level memorybarrier. This is BS's description of the volatile modifier in "TheC++ProgrammingLanguage":
Avolatilespecifierisahinttoacompilerthatanobjectmaychangeitsvalueinwaysnotspecifiedbythelanguagesothataggressiveoptimizationsmustbeavoided.
The volatile keyword is a type modifier that declares a type variable that can be changed by factors unknown to the compiler, such as the operating system, hardware, or other threads. When you encounter a variable declared by this keyword, the compiler no longer optimizes the code that accesses the variable, thus providing stable access to a particular address. Syntax for declaration: when intvolatilevInt; asks for the value of a variable declared by volatile, the system always re-reads data from its memory, even if its previous instructions have just read data from there. And the data read is saved immediately. For example:
Volatileinti=10
Inta=i
...
/ / other code, which does not explicitly tell the compiler that I has been manipulated
Intb=i
Volatile points out that I can change at any time and must be read from the address of I every time it is used, so the assembly code generated by the compiler will re-read data from the address of I and put it in b. The optimization practice is that because the compiler finds that the code between the two code that reads data from I has not operated on I, it automatically puts the last read data in b. Instead of rereading from I. In this way, if I is a register variable or represents a port data, it is prone to error, so volatile can guarantee stable access to a particular address. Note that in VC6, the general debug mode does not optimize the code, so the role of this keyword is not visible.
2. Volatile pointer
Similar to the const modifier, const has a term for constant pointers and pointer constants, and volatile has corresponding concepts:
Modifies that the object or data pointed to by the pointer is const or volatile:
Constchar*cpch
Volatilechar*vpch
Note: for VC, this feature implementation is not safe until after VC8.
The value of the pointer itself-- an integer variable that represents an address, which is const or volatile:
Char*constpchc
Char*volatilepchv
Note:
(1) you can assign a non-volatileint to a volatileint, but you cannot assign a non-volatile object to a volatile object.
(2) in addition to basic types, user-defined types can also be modified with volatile types.
(3) A class with an volatile identifier in C++ can only access a subset of its interface, a subset controlled by the implementer of the class. Users can only use const_cast to get full access to the type interface. In addition, volatile, like const, is passed from the class to its members.
At this point, the study of "what is the use of volatile in C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.