English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

3 answers

hello srinivasan!

An object's type may have additional qualifiers.Declaring an object const announces tht its value willn't b changed;declaring it *volatile
announces tht it has special properties relevant to optimization.niether qualifier affects the range of values or airthemetic properties of the object.

type qualifiers:
# const
# volatile

Type qualifier may appear with any type specifier.A const may be initialized,but not thereafter assigned to.There are no implementation -independent semantics for volatile objects.

const &volatile properties are new with the ANSI std.
purpose of *const - is to announce objects tht may be placed in read-only memory,& perhaps to increase opportunities for optimization.
The purpose of *volatile* -is to force an implementation to suppress optimization tht could otherwise occur.For example,
for a machine with memory mapped input/output,a pointer to a device register might be declared as a pointer to volatile,in order to prevent the compiler from removing apparently redundant refrences through the pointer.Except tht it shld diagnose explict attempts to change const objects, a compiler may ignore these qualifiers.

hope this would satisfy your urge for knowledge!! :)

2006-09-23 02:31:07 · answer #1 · answered by Neeraj Yadav♥ 6 · 0 1

Actually, the suppression of compiler optimizations is a side effect of the volatile keyword. And it will only affect optimizations that deal with the expected stability of the value of any given variable. The volatile keyword flags the variable's value as one that *MUST* be evaluated each time by the code instructions as written. In other words, while a compiler might ordinarily *ASSUME* that the contents of a value that it created in scope and set to, oh, say 1345, will still be that value a couple of ticks later and that the value is *AUTOMATICALLY* still the same as the register that contained the value to load into memory. While the compiler might ordinarily optimize the code so that the register content is used (faster, no memory access required) again in the next operation (see C specs for auto storage class, it automatically tries to store things in a register if available). This would be all wrong if, as pointed out by other posts, the value were to change due to an outside library routine accessing a variable set up to recieve its output. So, you can see that the volatile keyword does sometimes affect optimizations, but its true intention is to let the compiler know that this variable must be treated as changed each time it is accessed, whether the code in the program changed it or not, it may be changed without warning. I hope this helps explain the compiler's treatment of the keyword volatile.

2006-09-23 04:21:04 · answer #2 · answered by griz803 5 · 0 0

C Compilers often perform optimisation of code while building your image files. Volatile key word simply specifies that this variable can be accessed from external interface like Hardware interrupts, memory mapped IO and prevents compiler from performing optimisations. For ex:

I have a function

X()
{
I do a++;
..............// Don't access a anywhere in code
I do a--;
}

If a is being read in external interface as memory mapped IO which can interrupt execution of this function you get different values of a in between both. But if you don't declare it as volatile compiler will remove both a++ and a-- statements as they have cancelling efffects.

Hope this helps :)

2006-09-23 02:13:27 · answer #3 · answered by Anonymous · 0 0

fedest.com, questions and answers