i think it's the programmer's sanity
or there's the point about scope: a variable has meaning within a certain scope (function, loop, whatever), and the minute you leave that scope it ceases to be meaningful.
2007-02-04 04:44:41
·
answer #1
·
answered by wild_eep 6
·
0⤊
2⤋
Use of volatile
The compilation system tries to reduce code size and execution time on all machines, by optimizing code. It is programmer responsibility to inform compilation system that certain code (or variable) should not be optimized. Many programmers do not understand when to use volatile to inform compilation system that certain code should not be optimized or what it does. Although (no doubt) their program work, they do not exploit the full power of the language.
A variable should be declared volatile whenever its value can be changed by something beyond the control of the program in which it appears, such as a concurrently executing thread. Volatile, can appear only once in a declaration with any type specifier; however, they cannot appear after the first comma in a multiple item declaration. For example, the following declarations are legal
/* Let T denotes some data type */
typedef volatile T i;
volatile T i;
T volatile i ;
And the following declaration is illegal
T i, volatile vi ;
Volatile qualifiers can be used to change the behavior of a type. For example,
volatile int p = 3;
declares and initializes an object with type volatile int whose value will be always read from memory.
Syntax
Keyword volatile can be placed before or after the data type in the variable definition. For example following declaration are identical:
Volatile T a =3;
T volatile a=3;
The declaration declares and initializes an objet with type volatile T whose value will be always read from memory
Pointer declaration
Volatile T * ptr;
T volatile * ptr;
Ptr is a a pointer to a volatile T:
volatile pointer to a volatile variable
int volatile * volatile ptr;
volatile can be applied to derived types such as an array type ,in that case, the element is qualified, not the array type. When applied to struct types entire contents of the struct become volatile. You can apply the volatile qualifier to the individual members of the struct. Type qualifiers are relevant only when accessing identifiers as l-values in expressions. volatile does not affects the range of values or arithmetic properties of the object.. To declare the item pointed to by the pointer as volatile, use a declaration of the form:
volatile T *vptr;
To declare the value of the pointer - that is, the actual address stored in the pointer - as volatile, use a declaration of the form:
T* volatile ptrv;
Use of volatile
- An object that is a memory-mapped I/O port
- An object variable that is shared between multiple concurrent processes
- An object that is modified by an interrupt service routine
- An automatic object declared in a function that calls setjmp and whose value is-changed between the call to setjmp and a corresponding call to longjmp
for more visit
http://www.programmersheaven.com/articles/pathak/article1.htm
2007-02-04 12:57:00
·
answer #2
·
answered by Anonymous
·
1⤊
0⤋
It indicates that a variable may be changed in a way which is absolutely unpredictable by analysing the normal program flow (for example, a variable which may be changed by an interrupt handler).
Totally opposite of const
2007-02-04 12:45:07
·
answer #3
·
answered by scorpionwhiz 2
·
0⤊
0⤋