r_o_h_i_t wrote:
Can anybody please tell me the complete properties if volatile
variables in C.
This is the important one:
"An object that has volatile-qualified type may be modified in ways unknown
to the implementation or have other unknown side effects." (Taken from C99,
6.7.3(6))
And what are specific uses of them?
One fairly typical place in which you might need to use a volatile object
would be where you need to point to, say, a system clock. Imagine, for
example, an embedded system which has a particular 32-bits-wide memory
location containing the time, using 5 bits for the hours, 6 for the
minutes, 6 for the seconds, and - um - 15 for the 32768ths of a second. :-)
The only support the implementation provides for the clock is a function
returning its address. Thus:
volatile unsigned long *sysclock = _GetSystemClockAddress();
Now, your code might have something to do which will only take a few
microseconds, but it wants to wait until a tick has just happened, so that
it can do <whatever> without having the clock change under it. It could do
something like this:
while(*sysclock == *sysclock)
{
continue;
}
/* the clock just ticked */
If sysclock were not volatile-qualified, the compiler would almost certainly
treat this as a forever-loop. Not good! But because it's
volatile-qualified, the compiler can't assume the value of *sysclock won't
change during the evaluation of the condition. So (modulo poor
explanations!) it not only has to read *sysclock every time it goes round
the loop, but it actually has to read it /twice/. The volatile keyword
forces this. Consequently, in this case, when the clock goes "tick", the
loop will end - assuming the processor is fast enough to catch a tick "in
progress" (If it isn't fast enough, we might just have a forever loop after
all. Embedded programming is harder than it looks.)
--
Richard Heathfield :
bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ:
http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc:
http://users.powernet.co.uk/eton