Volatile keywords in C Language

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 in some compilers.

When variable I declared with the volatile keyword is accessed every time, the execution part retrieves the I value from the corresponding memory unit of I.

Variable I without the volatile keyword declaration may be taken directly from the cpu register when being accessed (because I was previously accessed, that is to say, the I value is retrieved from the memory and saved to a register.) The reason why the I value is directly taken from the register, rather than in the memory, the result of the compiler optimization code (access to the cpu register is much faster than access to ram ).

The difference between the two cases is that after compiled into assembly code, the two are different. This is because variable I may change frequently to ensure stable access to special addresses.

===== Reprinted below ======

The volatile keyword is a type modifier that uses the type variables it declares to indicate that it can be changed by unknown factors in some compilers.

Such as operating system, hardware, or other threads. When the variable declared by this keyword is met, the compiler

The code is no longer optimized 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 its memory again, that is

So that the previous command has just read data from this place. 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.

The assembler code generated by the interpreter reads data from the I address again and stores the data in B. The optimization method is because the compiler finds that

The code between the codes that I read data does not perform operations on I. It will automatically put the data that I read last time in B. Instead of restarting

Read from I. In this way, if I is a register variable or indicates a port data, it is prone to errors, so vola

Tile ensures stable access to special addresses.
Note: In vc6, the Code is not optimized in general debugging mode, so the function of this keyword cannot be seen. Below

Insert the assembly code to test whether the volatile keyword exists and the impact on the final code of the program:
First, use classwizard to create a win32 console project, insert a voltest. cpp file, and enter

Code:
 
# I nclude <stdio. h>
Void main ()
{
Int I = 10;
Int a = I;
 
Printf ("I = % d", );
// The purpose of the following Assembly statement is to change the I value in the memory, but it does not let the compiler know
_ Asm {
Mov dword ptr [ebp-4], 20 h
}
 
Int B = I;
Printf ("I = % d", B );
}
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:
# I nclude <stdio. h>
Void main ()
{
Volatile int I = 10;
Int a = I;
 
Printf ("I = % d", );
_ Asm {
Mov dword ptr [ebp-4], 20 h
}
 
Int B = I;
Printf ("I = % d", B );
}
Run the program in the debug version and release version respectively, and the output is:
I = 10
I = 32
This indicates that this keyword plays its role!

 

Related Article

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.