If a variable is classified as volatile, the compiler
will not optimize-out any reads or writes to that variable. This is usually done when you have a pointer and that pointer references a memory-mapped device. In other words, your pointer locates a device's control register. Storing a value would likely cause the hardware to perform some action. Likewise, reading from the location could provide a status of the device, or might dequeue some data from the device.
Under normal circumstances, this code:
Code:
char *some_var_pointer;
...
*some_var_pointer = 0x1B;
*some_var_pointer = 0xF2;
would be replaced with:
Code:
char *some_var_pointer;
...
*some_car_pointer = 0xF2;
because the compiler thinks you are just overwriting the previous value; that the 0x1B was an unnecessary write. Using:
Code:
volatile char *some_var_pointer;
will force the machine to perform both assignments.