Sunday, December 04, 2005

Volatile Pointer

Volatile is a variable which may change the valuewithout knowing of code.
A variable should be declared volatile whenever its value could change unexpectedly.

In practice, only three types of variables could change:
Memory-mapped peripheral registers
Global variables modified by an interrupt service routine
Global variables within a multi-threaded application

It is also used to avoid the compiler optimisation.

consider the below code,
volatile int *vp=0x5565;
.
.
.
.

code


while ( *vp!= 0 )
{
..code
}
In the above case, the vp pointer value may change during the run time.
To ensure that the volatile keyword is used.

so that the value of vp will not read often by compiler.
so the optimisation of the compiler will stop.
But in the ordinary pointer it is not done.

No comments: