Answer:
# Include <iostream. h>
# Include <stdio. h>
Class volatilefun {
Public:
Int val;
Volatile int F8 ();
};
Volatile int volatilefun: F8 () {return val ;}
void main ()
{< br> volatilefun s;
S. val = 89;
volatile int I = S. f8 ();
I = 23; // added by myself, can also run, and changed the value of I
cout getchar ();
}
If you have any questions, contact us! Thank you.
---------------------------------------------------------------
1. Volatile ensures that the accessed variables are not optimized by the compiler.
For example, the requested variable A = 1. If the embedded assembly is used, the compiler is released.
Change content 1 of address
And if you do not add volatile, it's still the original 1.
If this parameter is added, the data is read from the address of.
4. Register operations must be added! General driverProgramThe port operation in also needs to be added. Volatile only ensures that every time you take the variable address, it is the value of the source address of this variable! For example, declare a variable uint16 portaddress = 0x0001; the followingCodePortaddress is no longer assigned a value. If volatile is not added, the system will take 1 directly every time you use the portaddress variable, instead of getting the value from the portaddress address, if portaddress is related to hardware, it may change its value due to hardware reasons (no longer 0x0001), resulting in an error in running!
My View:
Simply remove the compiler from any Optimization of this variable.
All operations on this data are read and written to the corresponding address and are not taken from the registers temporarily saved for optimization.