为什么不能将变量注册为全局变量? [英] Why cant register variables be made global?
问题描述
从站点读取时,您读到不能创建寄存器类型为global的全局变量,为什么会这样呢?来源:
While reading from a site a read that you can not make a global variable of type register.Why is it so? source: http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/regdef.htm
推荐答案
在理论中,您可以将处理器寄存器分配给全局范围变量-该寄存器只需要保留分配给该变量的值即可.在该程序的整个生命周期中.
In theory, you could allocate a processor register to a global scope variable - that register would simply have to remain allocated to that variable for the whole life of the program.
但是,C编译器通常在编译阶段无法看到整个程序-编写C标准是为了使每个翻译单元(大致对应于每个 .c
文件)可以独立于其他文件进行编译(随后将编译后的对象链接到程序中).这就是为什么不允许全局作用域寄存器变量的原因-编译器在编译 bc
时,无法知道在 ac
(因此, bc
中的功能必须将值保留在该寄存器中).
However, C compilers don't generally get to see the entire program during the compile phase - the C standard was written so that each translation unit (roughly corresponding to each .c
file) could be compiled independently of the others (with the compiled objects later linked into a program). This is why global scope register variables aren't allowed - when the compiler is compiling b.c
, it has no way to know that there was a global variable allocated to a register in a.c
(and that therefore functions in b.c
must preserve the value in that register).
这篇关于为什么不能将变量注册为全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!