Use of volatile in C Language

Source: Internet
Author: User

For more information, see http://gyy.iteye.com/blog/166678!

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 the value of this variable every time when using this variable, rather than using the backup stored in the register. The following are examples of volatile variables:

1) Hardware registers of parallel devices (for example, Status Registers)
2) non-automatic variables (non-automatic variables) accessed by the Program of an interrupted service subaccount
3) variables shared by several tasks in multi-threaded applications
embedded system programmers often deal with hardware, interruptions, RTOS, and so on, all of which require volatile variables. If you do not know volatile content, it will lead to disasters.
The following are related questions:
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:
int square (volatile int * PTR)
{< br> return * PTR ** PTR;
}< br> The following is the answer:
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 an interrupt service subroutine modifies a pointer to a buffer.
3) The Code contains a prank. 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:
int square (volatile int * PTR)
{< br> int A, B;
A = * PTR;
B = * PTR;
return a * B;
}< br> because the * PTR value may be unexpectedly changed, A and B may be different. As a result, the returned value of this Code may not be the expected square value! The correct code is as follows:
long square (volatile int * PTR)
{< br> int A;
A = * PTR;
return a * A;
}

========================================================== ==========================================================

Volatile is intended to be "changeable"
Since the speed of accessing registers is faster than that of RAM, the compiler generally reduces the access to external Ram. For example:

Static int I = 0;

Int main (void)
{
...
While (1)
{
If (I) dosomething ();
}
}

/* Interrupt service routine .*/
Void isr_2 (void)
{
I = 1;
}

The program is intended to call the dosomething function in main when isr_2 is interrupted. However, since the compiler judges that I has not been modified in the main function, therefore, you may only perform one read operation from I to a register, and then use only the "I copy" in the register for each if judgment. As a result, dosomething will never be called. If the variable is modified with volatile, the compiler ensures that the read and write operations on the variable are not optimized (certainly executed ). In this example, I should also describe this.

Generally, volatile is used in the following areas:
1. Volatile must be added to the variable modified in the interrupted service program for testing by other programs;
2. Volatile should be added to the labels shared by all tasks in a multi-task environment;
3. Volatile is also required for memory-mapped hardware registers, because each read/write operation may have different meanings;

In addition, in the above situations, we often need to consider data integrity at the same time (the reading half of the correlated symbols is interrupted and overwritten). In 1, we can achieve this through related interruptions, task Scheduling can be disabled in 2, and 3 can only rely on the good design of hardware.

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.