extern和挥发性的区别 [英] Difference between extern and volatile
问题描述
此问题关于易失性和外部变量和还编译器的优化之间的差。
This question regards the difference between the volatile and extern variable and also the compiler optimization.
一个外部变量在主文件中定义,在一个多个源文件中使用,如:
One extern variable defined in main file and used in one more source file, like this:
ExternTest.cpp:
ExternTest.cpp:
short ExtGlobal;
void Fun();
int _tmain(int argc, _TCHAR* argv[])
{
ExtGlobal=1000;
while (ExtGlobal < 2000)
{
Fun();
}
return 0;
}
Source1.cpp:
Source1.cpp:
extern short ExtGlobal;
void Fun()
{
ExtGlobal++;
}
在vs2012下面这个生成的程序集:
The assembly generated for this in the vs2012 as below:
ExternTest.cpp装配访问外部变量
ExternTest.cpp assembly for accessing the external variable
ExtGlobal=1000;
013913EE mov eax,3E8h
013913F3 mov word ptr ds:[01398130h],ax
while (ExtGlobal < 2000)
013913F9 movsx eax,word ptr ds:[1398130h]
01391400 cmp eax,7D0h
01391405 jge wmain+3Eh (0139140Eh)
Source.cpp大会修改外部变量
Source.cpp assembly for modifying the extern variable
ExtGlobal++;
0139145E mov ax,word ptr ds:[01398130h]
01391464 add ax,1
01391468 mov word ptr ds:[01398130h],ax
从上面的装配,在while循环变量ExtGlobal每次访问读取相应地址的值。如果我添加到动荡的外部变量生成相同的程序集code。在两个不同的线程挥发性使用率和外部变量的使用在两个不同的功能是一样的。
From the above assembly, every access to the variable "ExtGlobal" in the while loop reads the value from the corresponding address. If i add volatile to the external variable the same assembly code was generated. Volatile usage in two different threads and external variable usage in two different functions are same.
推荐答案
询问有关的extern
和挥发性
像询问花生和大猩猩。他们是完全无关的。
Asking about extern
and volatile
is like asking about peanuts and gorillas. They're completely unrelated.
的extern
只是用来告诉编译器,嘿,不要指望找到这个C文件中该符号的定义。让连接器修复它底。
extern
is used simply to tell the compiler, "Hey, don't expect to find the definition of this symbol in this C file. Let the linker fix it up at the end."
挥发性
基本上是告诉编译器,永远不要相信这个变量的值。即使你的只是的存储值从寄存器到内存位置,不要再使用该寄存器中的值 - 使从内存一定要重新阅读
volatile
essentially tells the compiler, "Never trust the value of this variable. Even if you just stored a value from a register to that memory location, don't re-use the value in the register - make sure to re-read it from memory."
如果你想看到挥发性
会导致产生不同的code,写了一系列的读取变量/写。
If you want to see that volatile
causes different code to be generated, write a series of reads/writes from the variable.
例如,在编制本的cygwin code,用的gcc -O1 -c
,
For example, compiling this code in cygwin, with gcc -O1 -c
,
int i;
void foo() {
i = 4;
i += 2;
i -= 1;
}
生成以下组件:
_foo proc near
mov dword ptr ds:_i, 5
retn
_foo endp
请注意,编译器知道结果会是什么,所以它只是一往直前和优化它。
Note that the compiler knew what the result would be, so it just went ahead and optimized it.
现在,加入挥发性
到 INT I
生成以下内容:
Now, adding volatile
to int i
generates the following:
public _foo
_foo proc near
mov dword ptr ds:_i, 4
mov eax, dword ptr ds:_i
add eax, 2
mov dword ptr ds:_i, eax
mov eax, dword ptr ds:_i
sub eax, 1
mov dword ptr ds:_i, eax
retn
_foo endp
编译器从不信任 i的值
和总是的从内存重新加载它。
The compiler never trusts the value of i
, and always re-loads it from memory.
这篇关于extern和挥发性的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!