The modifier volatile tells the compiler that a variable's value may be changed in ways
not explicitly specified by the program. For example, a global variable's address may
be passed to the operating system's clock routine and used to hold the real time of thesystem. In this situation, the contents of the variable are altered without any explicit
assignment statements in the program. This is important because most C/C++
compilers automatically optimize certain expressions by assuming that a variable's
content is unchanging if it does not occur on the left side of an assignment statement;
thus, it might not be reexamined each time it is referenced. Also, some compilers
change the order of evaluation of an expression during the compilation process. The
volatile modifier prevents these changes.
You can use const and volatile together. For example, if 0x30 is assumed to be the
value of a port that is changed by external conditions only, the following declaration
would prevent any possibility of accidental side effects:
const volatile char *port = (const volatile char *) 0x30;
not explicitly specified by the program. For example, a global variable's address may
be passed to the operating system's clock routine and used to hold the real time of thesystem. In this situation, the contents of the variable are altered without any explicit
assignment statements in the program. This is important because most C/C++
compilers automatically optimize certain expressions by assuming that a variable's
content is unchanging if it does not occur on the left side of an assignment statement;
thus, it might not be reexamined each time it is referenced. Also, some compilers
change the order of evaluation of an expression during the compilation process. The
volatile modifier prevents these changes.
You can use const and volatile together. For example, if 0x30 is assumed to be the
value of a port that is changed by external conditions only, the following declaration
would prevent any possibility of accidental side effects:
const volatile char *port = (const volatile char *) 0x30;
No comments:
Post a Comment