How to use volatile in C

Source: Internet
Author: User

Volatile affects compiler compilation results. It is pointed out that volatile variables may change at any time. computation related to volatile variables should not be optimized to avoid errors, (VC ++ will

For compilation optimization, variable-related operations with the volatile keyword will not be compiled and optimized .).

For example:

Copy codeThe Code is as follows: volatile int I = 10;
Int j = I;
...
Int k = I;

Volatile tells the compiler that I may change at any time and must be read from the address of I each time it is used, therefore, the executable code generated by the compiler will re-read data from the I address and put it in k. The optimization method is because the Compiler

It is found that the code between the two data reads from I has not performed any operations on I, and it will automatically put the last read data in k. Instead of reading from I again. In this way, if I is a register variable or represents a port

Error-prone, so volatile can ensure stable access to special addresses without errors.

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

A variable defined as volatile means that this variable may be unexpectedly changed, so that the compiler will not assume the value of this variable. Precisely, the optimizer must carefully re-read this variable every time it uses it.

The value of this variable instead of the backup saved in the register. The following are examples of volatile variables:

1) Hardware registers of parallel devices (for example, Status Registers)

2) Non-automatic variables that will be accessed in an interrupt service subroutine)

3) variables shared by several tasks in multi-threaded applications cannot answer this question.

I think this is the most basic problem to distinguish between C programmers and embedded system programmers. Embedded people often deal with hardware, interruptions, RTOS, etc. All of these require volatile variables. Do not know volatile content

This will cause disasters. If the subject correctly answers this question (well, I suspect it will be the case), I will go a little deeper to see if this guy understands the full importance of volatile.

1) can a parameter be const or volatile? Explain why.

2) can a pointer be volatile? Explain why.

3); what are the following function errors:

Copy codeThe Code is as follows: int square (volatile int * ptr)

{
Return * ptr ** ptr;
}

The answer is as follows:

1) Yes. One example is read-only status registers. It is volatile because it may be unexpectedly changed. It is const because the program should not try to modify it.

2); yes. Although this is not very common. One example is when a service subroutine repairs a pointer to a buffer.

3) This code is a little abnormal. The purpose of this Code is to return the pointer * ptr points to the square of the value. However, since * ptr points to a volatile parameter, the compiler will generate code similar to the following:

Copy codeThe Code is as follows: int square (volatile int * ptr)

{
Int a, B;
A = * ptr;
B = * ptr;
Return a * B;
}

* The value of * ptr may be unexpectedly changed, so a and B may be different. As a result, this Code may not return the expected square value! The correct code is as follows:Copy codeCode: long square (volatile int * ptr)
{
Int;
A = * ptr;
Return a *;
}

Bit manipulation)

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

The keyword volatile is often used in embedded programming. The usage of volatile can be summarized as follows:

I. Tell compiler not to perform any Optimization

For example, to send two commands to a specific address:

Copy codeThe Code is as follows: int * ip =...; // device address

* Ip = 1; // The First Command

* Ip = 2; // The Second instruction.

The compiler program above may be optimized:

Int * ip = ...;

* Ip = 2;

The first command is lost.

If volatile is used, compiler will not allow any optimization, so as to ensure the program's original intention:

Volatile int * ip = ...;

* Ip = 1; * ip = 2;

Even if you want compiler to optimize it, it will not convert the two payment statements into one. It can only perform other optimizations. This is useful to device driver programmers.

2: variables defined with volatile will be changed outside the program. Each time they are read from the memory, they cannot be reused in the cache or register.

For example

Copy codeThe Code is as follows: volatile char;

A = 0;

While (! A)

{// Do some things ;}

Doother ();

If there is no volatile doother (), it will not be 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.