Volatile keyword is intended to prevent the compiler from applying certain optimizations which it might have otherwise applied because ordinarily it is assumed variables cannot change value "on their own."
If the comment section marked "OTHER INSTRUCTIONS" in above example never used the value pointed by "x", then the first assignment of * x = 1 is probably not required and could be removed. Yet a compiler which did not perform optimization would not recognize this fact and * x = 1 command would still end up being included in the executable.
End result of these optimizations is more efficient executables and a smaller required memory footprint.
Consider what happens if the memory pointed by variable "x" was a control line for an external device. Also imagine that sending a value of 1 to that memory told our ficticious device to begin some operation and that sending a value of 2 told it to stop. If the compiler did optimization and removed the * x = 1 instruction then the external device would never receive a start signal. Solution is to use the "volatile" type qualifier in the definition of the variable in Question.
When the keyword volatile is used in the type definition it is giving an indication to the compiler on how it should handle the variable. Primarily it is telling the compiler that the value of the variable may change at any
time as a result of actions external to the program. Once the compiler knows this it will also know that no operations involving the variable should be optimized out of the code no matter how extraneous they may appear to the optimization algorithm.
Please comment if you like the above post or find any mistake .
int * x;
...
void func( )
{
* x = 1;
/* OTHER INSTRUCTIONS */
* x = 2;
}
...
func( );
If the comment section marked "OTHER INSTRUCTIONS" in above example never used the value pointed by "x", then the first assignment of * x = 1 is probably not required and could be removed. Yet a compiler which did not perform optimization would not recognize this fact and * x = 1 command would still end up being included in the executable.
End result of these optimizations is more efficient executables and a smaller required memory footprint.
Consider what happens if the memory pointed by variable "x" was a control line for an external device. Also imagine that sending a value of 1 to that memory told our ficticious device to begin some operation and that sending a value of 2 told it to stop. If the compiler did optimization and removed the * x = 1 instruction then the external device would never receive a start signal. Solution is to use the "volatile" type qualifier in the definition of the variable in Question.
When the keyword volatile is used in the type definition it is giving an indication to the compiler on how it should handle the variable. Primarily it is telling the compiler that the value of the variable may change at any
time as a result of actions external to the program. Once the compiler knows this it will also know that no operations involving the variable should be optimized out of the code no matter how extraneous they may appear to the optimization algorithm.
Please comment if you like the above post or find any mistake .
No comments:
Post a Comment