Type modifier volatile keyword

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 met, the compilerCodeSo as to provide stable access to special addresses.

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 the memory where it is located, 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 that does not explicitly tell the compiler to perform operations on I
Int B = I;
>>>> Volatile indicates that I may change at any time. Each time you use it, you must read it from the I address, 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.
>>>> Note: In vc6, code optimization is not performed in the general debugging mode, so the function of this keyword cannot be seen. The following code is inserted to test whether the volatile keyword exists.ProgramImpact of final code:
>>>> First, use classwizard to create a Win32 console project, insert a test. cpp file, and enter the following code:
>

# Include "stdafx. H "# include <stdio. h> int main (INT argc, char * argv []) {int I = 10; int A = I; printf ("I = % d \ n", ); // The Role Of The following Assembly statement is to change the I value in the memory, but do not let the compiler know _ ASM {mov dword ptr [ebp-4], 20 h} int B = I; printf ("I = % d \ n", B); getchar (); Return 0 ;}

Then, run the program in debug version mode and the output result is as follows:
I = 10
I = 32
Then, run the program in release version mode. The output result is as follows:
I = 10
I = 10
The output results obviously show that in the release mode, the compiler optimizes the code and does not output the correct I value for the second time.

 

Next, we add the volatile keyword to the I statement to see what changes have taken place:

 
# Include "stdafx. H "# include <stdio. h> int main (INT argc, char * argv []) {volatile int I = 10; int A = I; printf ("I = % d \ n", ); // The Role Of The following Assembly statement is to change the I value in the memory, but do not let the compiler know _ ASM {mov dword ptr [ebp-4], 20 h} int B = I; printf ("I = % d \ n", B); getchar (); Return 0 ;}

Run the program in debug and release respectively, and the output is:

I = 10
I = 32
This indicates that this keyword plays 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.