Use of volatile

Source: Internet
Author: User

Volatile affects compiler compilation results, indicating that volatile variables are readily possible to change, and volatile variables related to the operation, do not compile optimization, to avoid errors, (VC + + in the production of release version executable code will be compiled optimization, Variables related to the volatile keyword will not be optimized for compilation. )。
For example:
volatile int i=10;
int j = i;
...
int k = i;
Volatile tells the compiler that I can change at any time, and each time it is used it must be read from the address of I, so the compiler generated executable code will re-read the data from I's address in K.
The optimization approach is that since the compiler found that the code between the I-read data two times does not operate on I, it automatically places the last-read data in K. Instead of re-reading from inside I. Thus, if I is a register variable or represents a port data is prone to error, so volatile can guarantee a stable access to the special address, no error.

/**********************

A variable that is defined as volatile means that the variable may be unexpectedly changed so that the compiler does not assume the value of the variable. Precisely, the optimizer must carefully re-read the value of the variable each time it uses the variable, rather than using the backup stored in the register. Here are a few examples of volatile variables:
1) Hardware registers for parallel devices (e.g., status registers)
2) non-automatic variables that are accessed in an interrupt service subroutine (non-automatic variables)
3) variables shared by several tasks in multi-threaded applications
The person who cannot answer the question will not be hired. I think this is the most basic problem of distinguishing between C 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. Assuming that the interviewee has answered the question correctly (well, wondering if this is the case), I'll look at it a little bit and see if this guy is straight on about 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:
int square (volatile int *ptr)
{
return *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:
int square (volatile int *ptr)
{
int A, B;
A = *ptr;
b = *ptr;
return a * b;
}
Because the values of the *ptr can be unexpectedly changed, A and B may be different. As a result, this code may return to the square value you expect! The correct code is as follows:
Long Square (volatile int *ptr)
{
int A;
A = *ptr;
Return a * A;
}
Bit operation (bit manipulation)

//*********************

In embedded programming often use volatile this keyword, on-line check his usage can be attributed to the following two points:

One: Tell compiler not to do any optimization

For example, to send two instructions to an address:
int *ip = ...; Device Address
*ip = 1; First instruction
*ip = 2; A second instruction
The above program compiler may be optimized as follows:
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.

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

Use of volatile (RPM)

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.