C/C++ volatile

1.volatile

volatile keyword C / C ++ and const in correspondence, to modify variables. The volatile keyword is a type modifier, the type variables declared its representation can be changed some compilers unknown factors, such as: operating system, hardware or other threads and so on. This keyword is encountered variable declaration, the compiler of the variable access code will no longer be optimized, which can provide stable access to specific addresses. Syntax statement: int volatile vInt; when the required value of the variable declared volatile system memory is always re-read the data from it, even if it had preceding instruction just read data therefrom. And read data is immediately saved. E.g:

int i = 10 volatile;
int i = A;
...
// other code does not explicitly tell the compiler, i been operating for
int b = i;
volatile when i pointed out that it is possible to change at any time, every time you use it It must be read from an address i of the compilers generated assembly code re-read data from the address i is placed in b. Optimized practice, due to the compiler found between the two codes read code data i no operation carried out, it will automatically be placed last read data from b i. I rather than re-read from the inside. This way, if the variable i is a register or a port represent data on error-prone, so that volatile can guarantee stable access to specific addresses. Note that in VC 6, a general debug mode no code optimization, so the effect of this keyword can not see. By inserting the following assembly code to test for the volatile keyword, the final impact of the program code, enter the following code:

Examples
#include <stdio.h>

void main()
{
int i = 10;
int a = i;

printf("i = %d", a);

// 下面汇编语句的作用就是改变内存中 i 的值
// 但是又不让编译器知道
__asm {
    mov dword ptr [ebp-4], 20h
}

int b = i;
printf("i = %d", b);

}
Then, in Debug mode version of the program, the output results are as follows:

10 = I
I = 32
Then, in the Release version of the program mode, the output results are as follows:

= 10 i
i 10 =
output results clearly show, Release mode, the compiler for code optimization, the second is not output the correct value of i. Below, we declare i plus the volatile keyword, and see what happens:

Examples
#include <stdio.h>

void main()
{
volatile int i = 10;
int a = i;

printf("i = %d", a);
__asm {
    mov dword ptr [ebp-4], 20h
}

int b = i;
printf("i = %d", b);

}
, Respectively, the Release version Debug and run the program, the output is:

10 = I
I = 32
indicating that the volatile keyword play its role. In fact, not only inline assembly manipulate stack "in this way belong to the compiler does not recognize the variables change, the other may be more multi-threaded concurrent access to shared variables, one thread changes the value of a variable, how to make changes to the value of other thread visible in general, volatile in several places with the following:

  1. Variables for other programs detected the interrupt service routine changes need to add volatile;
  2. Multitasking environment shared among tasks is a sign should be added volatile;
  3. Memory mapped registers usually have added volatile hardware description, because each of its different reading and writing may sense;

volatile pointer
and const qualifier similar words, const have constant pointers and pointer constant argument, volatile there is a corresponding concept:

Modifying the object pointed to by the pointer, the data is volatile or const:

the CPCH char * const;
volatile char * V PCH;
Note: For VC, this feature is achieved after VC 8 is safe.

The pointer value itself - an integer variable representing the address, of a volatile or const:

char* const pchc;
char* volatile pchv;
注意:

(1) can be assigned to a non-volatile int volatile int, but not the non-volatile object is assigned a volatile object.
(2) In addition to the basic types of user-defined type can also be modified with a volatile type.
(3) C ++ identifier in a volatile access to it only a subset of the class interface, controlled by a subset of the class implementor. Users can only use const_cast to gain full access to the type of interface. In addition, volatile as the const passed from class to its members.

Published 23 original articles · won praise 34 · views 30000 +

Guess you like

Origin blog.csdn.net/alangaixiaoxiao/article/details/103239310