Why is volatile needed in c?
来源:互联网 发布:网络连接超时怎么处理 编辑:程序博客网 时间:2024/05/29 10:14
Volatile tells the compiler not to optimize anything that has to do with the volatile variable.
There is only one reason to use it: When you interface with hardware.
Let's say you have a little piece of hardware that is mapped into RAM somewhere and that has two addresses: a command port and a data port:
typedef struct{ int command; int data; int isbusy;} MyHardwareGadget;
Now you want to send some command:
void SendCommand (MyHardwareGadget * gadget, int command, int data){ // wait while the gadget is busy: while (gadget->isbusy) { // do nothing here. } // set data first: gadget->data = data; // writing the command starts the action: gadget->command = command;}
Looks easy, but it can fail because the compiler is free to change the order in which data and commands are written. This would cause our little gadget to issue commands with the previous data-value. Also take a look at the wait while busy loop. That one will be optimized out. The compiler will try to be clever, read the value of isbusy just once and then go into an infinite loop. That's not what you want.
The way to get around this is to declare the pointer gadget as volatile. This way the compiler is forced to do what you wrote. It can't remove the memory assignments, it can't cache variables in registers and it can't change the order of assignments either:
This is the correct version:
void SendCommand (volatile MyHardwareGadget * gadget, int command, int data) { // wait while the gadget is busy: while (gadget->isbusy) { // do nothing here. } // set data first: gadget->data = data; // writing the command starts the action: gadget->command = command; }43 down voteAnother use for
volatile
is signal handlers. If you have code like this:quit = 0;while (!quit){ /* very small loop which is completely visible to the compiler */}The compiler is allowed to notice the loop body does not touch the
quit
variable and convert the loop to awhile (true)
loop. Even if thequit
variable is set on the signal handler forSIGINT
andSIGTERM
; the compiler has no way to know that.However, if the
quit
variable is declaredvolatile
, the compiler is forced to load it every time, because it can be modified elsewhere. This is exactly what you want in this situation.18 down vote
volatile
tells the compiler that your variable may be changed by other means, than the code that is accessing it. e.g., it may be a I/O-mapped memory location. If this is not specified in such cases, some variable accesses can be optimised, e.g., its contents can be held in a register, and the memory location not read back in again.12 down vote
volatile
in C actually came into existence for the purpose of not cacheing the values of the variable automatically. It will tell the machine not to cache the value of this variable. So it will take the value of the givenvolatile
variable from the main memory every time it encounters it. This mechanism is used because at any time the value can be modified by the OS or any interrupt. So usingvolatile
will help us accessing the value afresh every time.3 down voteup vote 2 down voteThere are two uses. These are specially used more often in embedded development.
Compiler will not optimise the functions that uses variables that are defined with volatile keyword
Volatile is used to access exact memory locations in RAM, ROM, etc... This is used more often to control memory-mapped devices, access CPU registers and locate specific memory locations.
See examples with assembly listing.Re: Usage of C "volatile" Keyword in Embedded Development
Volatile is also useful, when you want to force the compiler not to optimize a specific code sequence (e.g. for writing a micro-benchmark).
- Why is volatile needed in c?
- why mutli volumes is needed
- why the redo log is not needed to backup in the RMAN backup?
- Why is a different operating system needed?
- SCSI:Why Auto contingent allegiance is needed?
- Why Web Services are needed in Industry
- 【Redis】Why is an Event Library needed at all?
- What is the GlobalSuppressions.cs/GlobalSuppressions.vb file and why is it needed
- In C arrays why is this true? a[5] == 5[a]
- Why is the size of an empty class not zero in C++?
- Help Needed in compilation when TARGET_BUILD_VARIANT is set as "user".
- volatile keyword in C
- Why is everyone in such a rush?
- Why is SymbologyControl missing symbols in XP?
- Why is .innerText not working in Firefox?
- volatile in java, c, c++
- Why are pointers used in C++?
- Why is php not in my /bin directory??
- Linux下更新android sdk(备忘录)
- Win32控制台应用程序如何调用另一个.exe
- 二维数组的传参问题
- OS X Yosemite下SVN Versions的使用问题。
- C语言函数指针数组
- Why is volatile needed in c?
- 匿名内部类
- Javascript: How to enumerate property of an object ?
- Linux sed命令详解
- 重新安装crm的时候报表服务器安装报错
- Learning OpenCV cvResize
- android事件处理机制
- 第五章 树和二叉树
- 创始人股份分配方法