Volatile use method in C _c language

Source: Internet
Author: User
Tags volatile

Volatile affect compiler compiler results, pointed out that volatile variable is at any time may change, and volatile variables related to the operation, do not compile optimization to avoid errors, (VC + + in the production version of the executable code will be

To compile optimizations, add volatile to the variables associated with the keyword, and will not compile optimizations. )。

For example:

Copy Code code as follows:

volatile int i=10;
int j = i;
...
int k = i;

Volatile tells the compiler I is likely to change at any time, each time using it must be read from the address of I, so the compiler generated executable code will be again from the address of I read the data in K. And the optimization approach is that because the compiler

The code found two times from I read the data did not operate on I, and it automatically placed the last read data in K. Rather than re reading from I. So, if I is a register variable or represents a port data

Error-prone, so that volatile can guarantee a stable access to special addresses, there will be no error.

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

A variable defined as volatile means that the variable may be unexpectedly altered so that the compiler does not assume the value of the variable. Precisely, the optimizer must be careful to reread the variable every time it uses it

The value of this variable, instead of using a backup stored in the register. Here are a few examples of volatile variables:

1 hardware registers of parallel devices (e.g. state registers)

2 non-automatic variable (non-automatic variables) that is accessed in an interrupt service subroutine

3 multiple-threaded applications are shared by several tasks the person who cannot answer this question will not be hired.

I think this is the most basic problem to differentiate between C programmers and embedded system programmers. Embedded guys often deal with hardware, interrupts, RTOs, and so on, all of which require volatile variables. Do not know the content of volatile

will bring disaster. Assuming the interviewer has answered the question correctly (well, doubt it will be), I'm going to delve a little deeper and see if this guy is straight and understands volatile's full importance.

1 can a parameter be either a const or a volatile? explain why.

2); Can a pointer be a volatile? explain why.

3); What is wrong with the following function:

Copy Code code as follows:

int square (volatile int *ptr)

{
return *ptr * *PTR;
}


Here is 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 medium service subroutine fixes the 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:

Copy Code code as follows:

int square (volatile int *ptr)

{
int a,b;
A = *ptr;
b = *ptr;
return a * b;
}


Because the value of the *ptr can be unexpectedly changed, A and B may be different. As a result, this code may not return the square value you expected! The correct code is as follows:
Copy Code code as follows:

Long Square (volatile int *ptr)
{
int A;
A = *ptr;
Return a * A;
}

Bitwise operation (bit manipulation)

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

Embedded programming often used volatile this keyword, online check his usage can be summed up in the following two points:

One: Tell compiler not to do any optimization

For example, to send a two instruction to an address:

Copy Code code as follows:

int *ip = ...; Device Address

*ip = 1; First instruction

*ip = 2; A second instruction


The above program compiler may do optimization:

int *ip = ...;

*ip = 2;

Result the first instruction is lost.

If using volatile, compiler is not allowed to do any optimization, thereby guaranteeing the original intention of the program:

volatile int *ip = ...;

*ip = 1; *ip = 2;

Even if you want to compiler to do optimization, it will not put two of the value statement into one. It can only do other optimizations. This is useful for device driver programmers.

Second: The variable defined with volatile will be changed outside the program, each time must be read from memory, and can not put him in the cache or register to reuse.

Such as

Copy Code code as follows:

volatile char A;

a=0;

while (!a)

{//do some things; }

DoOther ();


If no volatile doother () is not executed

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.