从一个网站阅读时,你不能写一个类型为register的全局变量。为什么? 来源: http : //publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/regdef.htm
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
最满意答案
理论上 ,您可以将处理器寄存器分配给全局范围变量 - 该寄存器在程序的整个生命周期中都必须保持分配给该变量。
但是,C编译器通常不会在编译阶段看到整个程序 - 编写C标准是为了使每个翻译单元 (大致对应于每个.c文件)能够独立于其他编译单元编译(使用编译对象后来链接成一个程序)。 这就是为什么全局作用域寄存器变量不被允许 - 当编译器编译bc ,它无法知道在ac中有一个全局变量被分配给一个寄存器(因此, bc函数必须保存该值寄存器)。
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.
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).
更多推荐
发布评论