Correctly understand the importance of volatile completely

1 Introduction

Volatile affects the output of the compiler's compilation. The volatile variable may change at any time. The operation related to the volatile variable should not be compiled and optimized to avoid mistakes. (VC++ will generate compiler optimization when generating the release version executable code, plus volatile Variables related to keyword operations will not be compiled and optimized.).

E.g:

2, volatile

Volatile tells the compiler that i can change at any time, each time it is used, it must be read from the address of i, so the executable code generated by the compiler will re-read the data from the address of i in k.

The optimization is that because the compiler finds that the code between the two codes that read the data from i does not operate on i, it automatically places the last read data in k. Instead of reading it from i again. In this way, if i is a register variable or represents a port data, it is prone to error, so that volatile can guarantee stable access to a special address without error.

A variable that is defined as volatile means that the variable may be changed unexpectedly, so that the compiler will not assume the value of the variable. Precisely speaking, the optimizer must use this variable carefully to re-read the value of this variable each time, rather than using the backup stored in the register. Here are some examples of volatile variables:

1) Hardware registers of parallel devices (eg status register)

2) Non-automatic variables that will be accessed in an interrupt service subroutine

3) Variables Shared by Several Tasks in Multithreaded Applications People who cannot answer this question will not be hired. I think this is the most basic problem to distinguish between C programmers and embedded system programmers. Embedded guys often deal with hardware, interrupts, RTOS, and so on, all of which require the use of volatile variables. Not knowing the content of volatile will bring disaster. Assume that the interviewee answered correctly that this is a problem (well, if suspicion would be the case), and I'll take a closer look and see if this guy is rightly aware of the importance of volatile completely.

1) Can a parameter be const or volatile? Explain why. 2) Can a pointer be volatile? Explain why. 3) What is wrong with the following function:

Here is the answer: 1) Yes. One example is a read-only status register. It is volatile because it can be changed unexpectedly. It is const because the program should not try to modify it. 2) Yes. Although this is not very common. An example is when a service subroutine fixes a pointer to a buffer. 3) This code is a bit perverted. The purpose of this code is to return the pointer *ptr to the square of the value, but since *ptr points to a volatile parameter, the compiler will produce code similar to the following:

Since the value of *ptr may be unexpectedly changed, a and b may be different. As a result, this code may not be the square value you expected! The correct code is as follows:

3, volatile usage

Frequently used in embedded programming volatile keyword, checked his usage under the internet can be summarized as the following two points:

A: Tell the compiler can not do any optimization

For example, to send two instructions to an address:

As a result, the first instruction is lost. If you use volatile, the compiler is not allowed to do any optimization, thus ensuring the original intention of the program:

Even if you want the compiler to do optimizations, it won't convert the two-valued statements into one. It can only do other optimizations. This is useful for device driver programmers.

Two: It means that the variable defined with volatile will be changed outside the program. Every time it must be read from memory, it cannot be reused in cache or register.

Ego E cigarette

Suizhou simi intelligent technology development co., LTD , https://www.msmvape.com