|
|
|
|
Download e-Books
Aptitude Interview Questions
Online Quiz
|
Home > C Interview Questions > When should the volatile modifier be used ?
The volatile modifier is a directive to the compiler’s optimizer that operations involving this variable should not be optimized in certain ways. There are two special cases in which use of the volatile modifier is desirable. The first case involves memory-mapped hardware (a device such as a graphics adaptor that appears to the computer’s hardware as if it were part of the computer’s memory), and the second involves shared memory (memory used by two or more programs running simultaneously).
Most computers have a set of registers that can be accessed faster than
the computer’s main memory. A good compiler will perform a kind of
optimization called redundant load and store removal. The compiler looks
for places in the code where it can either remove an instruction to load
data from memory because the value is already in a register, or remove
an instruction to store data to memory because the value can stay in a
register until it is changed again anyway.
time_t time_addition(volatile const struct timer *t, int a) In this code, the variable t-> value is actually a hardware counter that is being incremented as time passes. The function adds the value of a to x 1000 times, and it returns the amount the timer was incremented by while the 1000 additions were being performed. Without the volatile modifier, a clever optimizer might assume that the value of t does not change during the execution of the function, because there is no statement that explicitly changes it. In that case, there’s no need to read it from memory a second time and subtract it, because the answer will always be 0. The compiler might therefore optimize the function by
making it always return 0.
|