What is the use of volatile keyword? Give me one example?

Questions by ykirankumarmca

Showing Answers 1 - 2 of 2 Answers

minli

  • Apr 9th, 2006
 

(P876 from textbook C++ How To Program:)
The volatile type qualifier is applied to a definition of a variable that may be altered from outside the program (i.e., the variable is not completely under the control of the program). Thus, the compiler cannot perform optimizations (such as speeding program execution or reducing memory consumption, for example) that depend on "knowing a variable's behavior is influenced only by program activities the compiler can observe."

(notes:)
1) volatile indicate the object is modified by something not directly under the compiler's control (i.e., the hardware itself)
2) one use of volatile qualifier is to provide access to memory locations used by asynchronous processes such as interrupt handlers.
3) Another example might be the global variables that keeps track of the total number of timer interrrupts.

  Was this answer useful?  Yes

jay

  • Sep 17th, 2006
 

In general, volatile keyword signals compiler that the declared variable should not be optimized by any ways (e.g. it should not be cached, kept in registers etc but anytime it's used - it should be read/written directly from/to the memory).Most often you'll probably use volatile for data shared among threads and in various issues concerning iterrupts (it's too complex an issue to lay it all out here though)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions