The VOLATILE keyword.


I have had several sugestions on how to describe volatile, If you have any input please mail me.


Examples:

o No example:-(


We can try to explain the volatile keyword with an example: Let's say you define a variable var_A and give it an initial value. Later on in function_1() you multiply var_A by 2 an use it for something. Afterwards in function_2() you again multiply var_A by 2 an use it for something else. One possible optimization by the compiler could be to hold var_A in a register and double the value in the register. Then use the (already doubled) register value in func_1() and func_2(). This would save one multiplication! Now let's assume an interrupt occurs exactly between func_1() and func_2() and somewhere in the ISR you change var_A. Then the above optimization would lead to an incorrect result, as var_A is no longer the same for func1/2. The compiler cannot predict when the interrupt is to occur, so it might do the 'wrong' optimization ! In this case (and it's up to you to take care of this!) you use the volatile keyword, to tell the compiler that there might be a chance of changing a variable without notice, and therfore to prevent him from doing such optimizations !

See also:


Top Master Index Keywords Functions


Martin Leslie