为什么不能将变量注册为全局变量?(Why cant register variables be made global?)

编程入门 行业动态 更新时间:2024-10-28 19:32:38
为什么不能将变量注册为全局变量?(Why cant register variables be made global?)

从一个网站阅读时,你不能写一个类型为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).

更多推荐

本文发布于:2023-08-04 15:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能将   变量   全局变量   global   variables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!