Usage of C ++ volatile

Source: Internet
Author: User

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 (from memory) every time when using this variable, rather than using 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
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 people often deal with hardware, interruptions, RTOS, etc. All of these require volatile variables. If you do not know volatile content, it will lead to 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:
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:

C ++ code
  1. Int square (volatile int * PTR)
  2. {
  3. Int A, B;
  4. A = * PTR;
  5. B = * PTR;
  6. Return a * B;
  7. }



* 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:
C ++ code

  1. Long square (volatile int * PTR)
  2. {
  3. Int;
  4. A = * PTR;
  5. Return a *;
  6. }



Volatile is equivalent to telling the compiler that the variables declared by the compiler are changeable, uncertain, and may be changed by external programs (such as interrupt programs). The compiler is prohibited from optimizing its read/write operations, if:
Int I;
Then the compiler may optimize it and place it in the CPU register, which is good in most cases. However, in some cases, we will require some variables to be in the memory (such as drivers, interrupt handlers, and so on). In this case, the compiler optimization is a problem. To avoid this situation, we should define it as follows:
Volatile int I;

PS: Volatile is usually used to prevent Compiler Optimization operations. For example, you have an inaccurate latency function:
C ++ code

  1. Void delay (unsigned int timeout)
  2. {
  3. Unsigned int I;
  4. For (I = 0; I <timeout; I ++ );
  5. }


Some compilation will be smart enough to notice that this function does nothing in essence and will optimize the call to this function. However, this is wrong, so you should declare it as follows:
C ++ code

  1. Volatile void delay (...)
  2. {
  3. // Same as above
  4. }

Address: http://wujianjun0410.iteye.com/blog/615530

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.