One instance of this that I heard, is for use in a timer. Every time a interrupt activates, it sees if the time is now say 5:00. Because the loop is repeated so often the compiler will stash a copy of the first value in a register. In short, 5:00 will never come. This requires a volatile definition to tell the compiler that this variable can't ever be cached.
>________________________________
>From: Ravi Shankar <blr_ravi2006@yahoo.co.in>
>To: Programmers-Town@yahoogroups.com
>Sent: Saturday, July 30, 2011 11:59 AM
>Subject: (PT) Difference between const and volatile
>
>What is the difference between const and volatile variable ? In fact, everyone will be clear from const, but it is really difficult to understand about volatile keyword.
>A const variable is the variable, which can't be modified by the compiler. If you try to modify it, then compiler will give an error message. Suppose, we have a const variable as below.
>const int i = 10; i = 20 // error - compiler throws the variable can't be modified.
>But in case of volatile keyword, the variable declared as volatile tells the compiler that the variable can be modified anytime other than the compiler.
>Suppose, we are returning a volatile variable from a function. int func(){ volatile int i; i = 10; return (i*2);}
>Here we are expecting fun() function to return 20. but as we are declaring i as volatile, so between return (return (i*2) ) and assignment statement (i = 10), it might get modified to some different value and return value might differ from 20.
>Please clarify if anything i explain and explain in details to understand better. What is use of volatile keyword ? In some blog, i have seen - use of volatile keyword is mostly used in multi-threaded application and interrupt status application. but how is it used in these kind of application. Please clarify this too ? regards - Ravi
>
>[Non-text portions of this message have been removed]
>
>
>
>------------------------------------
>
>To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
>
>Yahoo! Groups Links
>
>
>
>
>
>
[Non-text portions of this message have been removed]
Tuesday, August 2, 2011
Re: (PT) Difference between const and volatile
__._,_.___
To unsubscribe : programmers-town-unsubscribe@yahoogroups.com
.
__,_._,___
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment