Code compilation Environment: Windows7 32bits+vs2012.
Volatile is the meaning of "variable" and "unstable". Volatile is a less-used keyword for C/s + +, which is used to solve the problem of variable read errors that can occur in a "shared" environment.
The role of 1.volatile
A variable defined as volatile is a variable that can be unexpectedly changed, and when the compiler optimizes it, it uses this variable to read the variable value from RAM every time, instead of using the backup stored in the register.
In a single-tasking environment, within a function body, if the statement between the values of the two read variables does not modify the value of the variable, the compiler will try to optimize the executable code. Since the access register is faster than RAM (reading the value of a variable from RAM to the register), the value of the variable is read from the register and not accessed by the RAM as long as the value of the variable is not changed.
In a multitasking environment, although within a function body, there is no modification of the value of the variable between two read variables, the variable can still be modified by other programs (such as interrupt programs, additional threads, and so on). If the fellow still reads the value of the variable from the register rather than from RAM, there will be a problem with the modified Biarron's inability to respond in a timely manner. The following procedure simulates this phenomenon:
#include <iostream>usingnamespacestd;int main(int argc,char* argv[]){ int i=10; int a=i; cout<<a<<endl; _asm{ mov dword ptr [ebp-4],80 } int b=i; cout<<b<<endl; getchar();}
The program generates release versions in the VS2012 environment, and the output is:
10
10
Read the above procedure and pay attention to the following points:
(1) The above code must be examined in release mode, because the program code is optimized only in release mode, and this optimization is prone to problems in variable sharing environments.
(2) Before the statement b=i, the value of I has been modified by the inline assembly code, but the change of I is not reflected in B, and if I is a variable shared by multiple tasks, the error caused by this optimization is likely to be fatal.
(3) The assembly code [EBP-4] represents the address of the variable I, because EBP is an extended base point pointer register that holds the stack-bottom address of the stack to which the function belongs. First into the stack, taking up 4 bytes, with the increase in the number of local variables declared in the function, ESP (stack top pointer register) will be correspondingly reduced, because the stack growth direction from high address to low address growth. I is the first variable, taking up 4 bytes, so I's address is [Ebp-i].
So how do you suppress the compiler's optimization of read variables to prevent erroneous reads?
Volatile can be easily qualified, the above program slightly modified, the variable i before the declaration as volatile, observe the following procedures:
#include <iostream>usingnamespacestd;int main(int argc,char* argv[]){ volatileint i=10; int a=i; cout<<a<<endl; _asm{ mov dword ptr [ebp-4],80 } int b=i; cout<<b<<endl; getchar();}
The program output results are:
10
80
That is, the second time the value of the variable i is read, the value after the change has been obtained. The trace assembly code shows that any variable that is declared volatile is read from memory each time the value of the variable, rather than in some cases directly from the register.
2. Application of volatile variables
(1) Hardware registers for parallel devices (e.g., status registers);
(2) A non-automatic variable (non-automatic variables) that will be accessed in an interrupt service subroutine;
(3) A variable that is shared by several tasks in a multithreaded application.
The people above are the most basic questions that distinguish between C/s + + programmers and embedded system programmers. Embedded guys often deal with hardware, interrupts, RTOs, and so on, all of which require volatile variables. The content that does not understand volatile will bring disaster.
The following questions can be seen whether the interviewer is directly aware of the importance of volatile.
1) can a parameter be either const or volatile? explain why.
2) Can a pointer be volatile? explain why.
3) What is wrong with the following function:
intint*ptrreturn*ptr*ptr
Here's the answer:
1) Yes. An example is a read-only status register. It is volatile because it can be changed unexpectedly. It is const because the program should not attempt to modify it.
2) Yes. Although this is not very common. An example is when a service subroutine fixes a pointer that points to a buffer.
3) This piece of 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:
intint*ptrint*ptr*ptrreturn
Because the values of the *ptr can be unexpectedly changed, A and B may be different. As a result, this code may return
The square value you expect! The correct code is as follows:
longaareturnaa
3. The role of volatile in embedded programming
In embedded programming often use volatile this keyword, on-line check his usage can be attributed to the following two points:
(1) Tell compiler not to do any optimization
For example, to send two instructions to an address:
int *ip =...; //设备地址 1; //第一个指令 2
The above program compiler may be optimized as follows:
...2
Results The first instruction is missing. In the case of volatile, compiler does not allow any optimizations to be made to ensure the program's original intent:
...12
Even if you want to compiler to optimize, it will not make two of the assignment statements between one, it can only do other optimizations. This is useful for device driver programmers.
(2) A variable defined with volatile is changed outside of the program and must be read from memory each time, and cannot be reused in the cache or register. Such as:
chara; a=0while(!a){ //do some things; }
If no volatile,doother () will not be executed
Reference documents:
[1]] Chen Gang. Advanced Step-by-step tutorials for C + + [M]. Wuhan: Wuhan University Press, 2008.
[2]http://www.cnblogs.com/chio/archive/2007/11/24/970632.html
[3]http://www.360doc.com/content/13/0309/22/9290626_270468335.shtml
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The use of volatile in C + +