Volatile-first acquaintance

Source: Internet
Author: User

The volatile keyword is a type modifier that uses the type variables it declares to indicate that it can be changed by unknown factors of Some compilers, such as operating systems, hardware, or other threads. When the variable declared by this keyword is encountered, the compiler will not optimize the code that accesses the variable, so as to provide stable access to the special address.

An example of using this keyword is as follows:
Int volatile nvint;
When the value of a variable declared by volatile is required, the system always reads data from its memory, even if the previous command has just read data from it. The read data is saved immediately.

For example:

Volatile int I = 10; int A = I;... // other code, not explicitly telling the compiler that I has been operated on Int B = I;
Volatile indicates that I may change at any time and must be read from the I address each time it is used, therefore, the compilation code generated by the compiler will read data from the I address again and put it in B. The optimization method is that because the compiler finds that the code between the codes that read data from I has not performed any operations on I, it will automatically put the data that was last read in B. Instead of reading from I again. In this way, if I is a register variable or indicates that data on a port is prone to errors, volatile can ensure stable access to special addresses.

××××××××××××××××××××××××××××××××××××××××××× ××

What does the keyword volatile mean? Three different examples are provided.
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 that will be accessed in an interrupt service subroutine)
3) variables shared by several tasks in multi-threaded applications
People who cannot answer this question will not be hired. I think this is the most basic problem to distinguish between C programmers and embedded system programmers. 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.
If the subject correctly answers this question (well, I doubt this will happen), 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:
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. (That is to say, const specifies that this variable cannot be changed in our program code, but volatile points out that this value can be accidentally changed in the Code due to hardware reasons, however, our code will also update and use this latest value)
2). Yes. Although this is not very common. One example is when a service subroutine repairs a pointer to a buffer. (Declaring a pointer As a volatile type ensures that the address pointed to by the pointer changes at any time)
3) This code has 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)
{
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:
Long square (volatile int * PTR)
{
Int;
A = * PTR;
Return a *;
}

 

*************** ****************

Volatile is intended to be "easy to change" because the speed of access registers is faster than ram, the compiler generally optimizes 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 the hardware registers mapped to memory, because each read/write to it may have different meanings;

Volatile indicates that the variable content may be changed when the program is unknown.
For example, the content of its memory address is interrupted by the function, or the content of other processes is changed.
This type of variable is not prefetch in the cache during program execution, but is directly obtained every time it is used.
For example, you write such a program in C.
For (INT I = 0; I <100000; I ++ );
Empty loop, nothing
This item will be optimized. If this mark is added before int, it will not be optimized, because each change of I is not necessarily changed by other programs in the middle of the loop.

In Linux, the source code (Linux/MM/memory. c) contains the following two sentences:
Volatile void do_exit (Long Code );

Static inline volatile void OOM (void)
{
Printk ("out of memory/n/R ");
Do_exit (SIGSEGV );
}

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.