Understanding of volatile

Source: Internet
Author: User

Hey. There are too many things to learn, too little time. A week of work to summarize the things too much, but also to deal with, the more accumulate. Great weekend to go out to play ah .... Get it, the crap ends here.

Boring time to look at the Web page found a volatile article, has previously been confused about volatile. So I'm learning it again http://blog.sina.com.cn/s/blog_559f6ffc01000aip.html

The

volatile keyword is a type modifier , which is represented by the type variable it declares
For example:
>>>> volatile   The value of a variable declared , the system always re-reads the data from the memory it is in even if the preceding instruction has just read the data from that location. And the read data is saved immediately. The
is not re-read from the register but is re-read from memory, for example:
volatile  int  I = 1 0;
int a =  i ;----->//read from memory
...//other code, not explicitly told to the compiler, to I Operation
...//other code, did not explicitly tell the compiler, the I did the Operation
Int b =  i ;-------> It does not read the value of I from the register but again reads the value of I from memory


>>>> volatile   point   I is subject to change at any time and must be read from the address of I each time it is used, so the compiler generated assembly code will re-read the data from the address of I in B. and I reads the data does not operate on the I , It will automatically put the last-read data in B. Instead of re-reading from I . since then, if I is a register variable or represents a port data error prone, it is said that volatile can guarantee stable access to special addresses .  

volatile variables may change in situations where your program does not know
such as multi-threaded programs, common access to the memory, a number of programs can manipulate this variable
Your own program is unable to determine when this variable will change
For example, he corresponds to a state of an external device, and when an external device is operating, through the driver and interrupt events, the system changes the value of the variable, and your program does not know it.
for volatile types of variables, each time the system uses it, it is extracted directly from the corresponding memory, rather than using the original value in the cache to accommodate changes in its unknown when it occurs.

A typical example
for (int i=0; i<100000; i++);
This statement is used to test the speed of an empty loop.
But the compiler is definitely going to optimize it and not execute it at all.
If you write
for (volatile int i=0; i<100000; i++);
It's going to do it.

A definition ofvolatileVariable is that the variable may be unexpectedly changed so that the compiler will 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. Below isvolatileA few examples of 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

Assuming that the interviewee answered the question correctly (well, wondering if this would be the case), I'll dig a little deeper and see if this guy knowsvolatileFull of importance.
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 isvolatileBecause 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). There's a prank on this piece of code. The purpose of this code is to return the pointer *ptr to the square of the value, however, since *ptr points to avolatileType parameter, the compiler will produce code similar to the following:
int Square (volatileint *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 (volatileint *ptr)
{
int A;
A = *ptr;
Return a * A;
}

The summary is that this variable is likely to change for shared variables, but this change is not the result of the program (code) you are interested in. In this case, the compiler might assume that the shared variable is not changed and that the value before the change is used directly. And when we add vialoate, we're telling the compiler, Do not directly use the last value, give me to read from inside the memory again!!

Understanding of volatile

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.