Description of the volatile keyword and testing

Source: Internet
Author: User
Tags printf volatile

The volatile keyword is a type modifier that declares a type variable that can be changed by factors unknown to some compilers, such as operating system, hardware, or other threads. When you encounter a variable that is declared by this keyword, the compiler will no longer optimize the code that accesses the variable, thus providing a stable access to a particular address.

Examples of using this keyword are as follows:

int volatile nvint;

When the value of a variable declared by volatile is required, the system always reads the data back from its memory, even if the previous instruction has just read the data from it. And the data being read is saved immediately.

For example:

volatile int i=10;
int a = i;
...
//其他代码,并未明确告诉编译器,对i进行过操作
int b = i;

Volatile points out that I is subject to change at any time and must be read from the address of I whenever it is used, so that the compiler-generated assembly code will again read the data from I to the address in B. The optimization approach is that because the compiler finds that the code in the code between the two times I read data does not operate on I, it automatically places the last read data in B. Rather than re reading from I. Thus, if I is a register variable or a port data is prone to error, so volatile can guarantee a stable access to special addresses.

Note that in VC6, the General debug mode is not optimized for code, so the role of this keyword cannot be seen. The following inserts the assembly code to test for the volatile keyword, the effect on the program final code:

First, build a Win32 console project with ClassWizard, insert a voltest.cpp file, and enter the following code:

#include <stdio.h>
void main()
{
  int i=10;
  int a = i;

  printf("i= %d\n",a);
  //下面汇编语句的作用就是改变内存中i的值,但是又不让编译器知道
  __asm {
    mov dword ptr [ebp-4], 20h
  }

  int b = i;
  printf("i= %d\n",b);
}

Then, run the program in debug version mode, and the output is as follows:

i = 10
i = 32

Then, run the program in Release version mode, and the output is as follows:

i = 10
i = 10

The results of the output show that, in release mode, the compiler optimizes the code and does not output the correct I value for the second time. Below, we add the declaration of I to the volatile keyword to see what changes:

#include <stdio.h>
void main()
{
  volatile int i=10;
  int a = i;

  printf("i= %d\n",a);
  __asm {
    mov dword ptr [ebp-4], 20h
  }

  int b = i;
  printf("i= %d\n",b);
}

Run the program separately in the debug and release versions, and the output is:

i = 10
i = 32

This shows that the keyword has played its role!

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.