C Language, volatile

Source: Internet
Author: User

First, meaning:

The meaning of this keyword is that the value of the defined variable will change at any time, and the value must be read from the address of the variable, so that only the variable may be changed during use (such as interrupting the program), it needs to be described by this keyword.

1 volatile, the keyword volatile ensures that this instruction is not omitted due to the optimization of the C compiler and requires each direct read value.

2 ) shared memory address, when multiple programs are operating on it. Your program does not know when this memory has been changed. If you do not add this voliatile modification, the program is using the data in the catch , that may be outdated, add voliatile, when needed, the program to go back to that address to extract, The guarantee is up to date .

—————————————————————————————————————

Second, the use of the scene

volatile mainly used in cases where variables are changed asynchronously , there are three main aspects

1) . hardware registers for parallel devices ( CPU Peripheral registers ) (e.g., status registers)

2) . Global variables used by interrupts and main loops  

3). Common variables that are used between threads in the operating system .

———————————————————————————————————

third, the embedded programming often use the keyword volatile , the following are two examples of usage:[1]

1): Tell compiler not to do any optimizations

For example, to send two instructions to an address:
int *ip = ... ; //Device Address
*ip = 1;// First instruction
*ip = 2;// A second instruction
The above procedurescompiler may be optimized as:
int *ip = ... ;
*ip = 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:
volatile int *ip = ... ;
*ip = 1;
*ip = 2;
Even if you want to compiler optimization, it will not be two times the value of the statement into one. It can only do other optimizations. This is useful for device driver programmers.

Another example: [2]

xbyte[2]=0x55;

xbyte[2]=0x56;

xbyte[2]=0x57;

xbyte[2]=0x58;

If the above four statements of the external hardware represent different operations, there will be four different actions, then the compiler will not be as pure as the program to optimize the above four statements are considered xbyte[2]=0x58, and ignore the first three statements, and only produce 1 machine code. If Xbyte is modified with Volatileo, the compiler compiles each one at a time and produces the corresponding 4 machine codes.

2): The 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 volatile char a;
a=0;
while (!a) {
Do some things;
}
DoOther ();
If no volatile doother () will not be executed

Four

    1. Volatile vs. const

volatile and const are used together, there is no conflict, and theconst is guaranteed to be the same for this program;volatile to cope with other conditions (such as multi-threading, interrupts), Avoid errors after optimization.

    1. volatile and pointers  

The volatile modifier pointer is generally used on the shared pointer.

The following code:

UCHAR * Volatile reg;
in the line of codevolatilethe modification isRegthis variable. So here's actually a definition of aUchartype of pointer, and the pointer variable itself isvolatilethe. But what the pointers refer to is notvolatilethe! When actually used, the compiler pairs the pointer variables in the codeRegThe operation itself is not optimized, but theRegwhat is meant byRegbut will act asNon-volatilecontent processing, forRegoperation is still optimized. usually this is used in the declaration of a shared pointer, that is, the pointer variable may be interrupted and other functions modified. define it asvolatilelater, the compiler will load the value of the pointer variable every time, so that even if the variable has been modified by another program to use the current function can also get the modified value (otherwise, it is usually only in the beginning of the function is taken in the register, and then always use a copy in the register).

It is important to note that the above code differs from the following code

volatile Uchar *reg;
in this line of codevolatileThe modifier is what the pointer refers to. So here's a definition of aUchartype of pointer, and this pointer is pointing to avolatilethe object. But the pointer variable itself is notvolatilethe. If the pointer variableRegoperations such as calculations or assignments are likely to be optimized by the compiler. But toRegwhat you point toRegthe compiler optimizations are forbidden by the reference. Because this pointer is referring to avolatileobject, the compiler must ensure that the Reg operations are not optimized. Typically in driver development, the definition of a hardware register pointer should take this form .

and
Volatile Uchar * volatile reg;
so the defined pointer is itself a volatile variable that points to the volatile the data content.

Reference:

1. http://blog.csdn.net/turkeyzhou/article/details/8953911

Reference

1. Volatile usage in C http://www.cnblogs.com/chio/archive/2007/11/24/970632.html

2. Volatile related knowledge http://www.100ask.org/bbs/forum.php?mod=viewthread&tid=3902&highlight=volatile

  

C Language, volatile

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.