变量的内存分配 [英] Memory allocation of variables

查看:101
本文介绍了变量的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在混乱。是否自动,静态和全局变量的分配发生在编译​​时或运行时?

I'm in confusion. Does the allocation of automatic, static and global variables take place at compile time or run time?

我所知道的事情是,在编译时,源$ C ​​$ C被翻译成机器语言。

The thing I know is that at compile time, the source code is translated to machine language.

在编译器发现像 int类型的陈述; ,它写入指令。做任何额外的事情发生,例如内存分配,在编译时?

When the compiler finds a statement like int a;, it writes the instruction. Do any extra things happen, like memory allocation, at compile time?

在.exe文件将被执行会发生什么事?

What happens when the .exe file will be executed?

无论计算机(OS)或编译器将分配在运行时保持一个整数足够的内存或编译的时间。

Whether computer (OS) or compiler will allocate sufficient memory of holding an integer at runtime or compile time.

同时它是说,一个全局变量的地址是一个编译时间常数。这是什么意思?
请帮忙解决每一个问题,特别是最后一个。

Also it is said that the address of a global variable is a compile-time constant. What does it mean? Please help to solve each question, especially the last one.

推荐答案

静态全局变量分配的内存资源,无论是在编译时或运行时。这依赖于静态变量是否是零初始化,或者如果他们有初始常数值。例如,code像

Static global variables are allocated memory resources either at compile-time or run-time. This depends on whether the static variables are zero-initialized, or if they have initial constant values. For instance, code like

//global variable with internal linkage
static int array[100];

不会占用可执行任何一个房间......换句话说可执行该阵列,因为它不包含任何编译器/连接器将不会分配内存。它会留下一个存根,虽然,表明该可执行文件启动,内存必须为数组分配。一旦启动可执行时,OS看到由链接器留下的存根,和分配和零初始化为数组的存储器(以及其他存储器堆等)。在另一手,

won't take up any room in the executable ... in other words the compiler/linker will not allocate memory in the executable for that array since it doesn't contain anything. It will leave a stub though that indicates when the executable is launched, memory must be allocated for the array. Once you launch the executable, the OS sees the stub left by the linker, and allocates and zero-initializes the memory for the array (as well as other memory for the heap, etc.). On the other-hand,

//global variable with internal linkage
static int array[100] = { 1, 2, 3};

因为它是在编译时具有恒定值初始化将占用空间的可执行文件。因此,编译器将发出在数据组件的部分文件,它生成的数组分配存储空间code。然后链接器将正确的布局,被链接到最终的可执行文件中的所有组件文件的数据段和code部分。当操作系统加载可执行文件到内存中,数组内存已经是可执行的记忆脚打印。

will take up space in the executable since it is initialized with constant values at compile-time. Thus the compiler will emit code in the data section of the assembly file it generates that allocates storage for the array. The linker will then properly layout the data-section and code sections of all the assembly files that are being linked into the final executable. When the OS loads the executable into memory, the memory for the array is already part of the executable's memory "foot-print".

自动变量,因为它们是code的执行期间在栈上分配,是在运行时间分配。

Automatic variables, since they are allocated on the stack during the execution of code, are allocated at run-time.

此外,它说,全局变量的地址是编译时间常数。

Also it is said that address of global variable is compile time constant.

这是一个有点误导...用C你可以不知道任何全局变量的确切内存地址,直到连接器创建可执行文件,操作系统加载可执行文件到内存中。可以这样做的唯一方法是,如果你手工组装的文件,并创建了一个专门加载到操作系统中的给定地址的平坦二进制,但现代操作系统不让你做。相反,全局变量的地址由接头给出占位,以便它们可以与时OS加载在运行时可执行正确的值取代。因此,尽管内存地址是在意义上的常数,该程序运行时,它不会随着时间而改变,它的实际价值是不是在编译时分配的。

That's a bit misleading ... in C you can't know the exact memory-address of any global variables until the linker has created the executable, and the OS has loaded the executable into memory. The only way this could be done would be if you hand-assembled a file and created a flat-binary that was specifically loaded into a given address by the operating system, but modern operating systems don't let you do that. Instead, the addresses of global variables are given place-holders by the linker so that they can be substituted with the correct values when the OS loads the executable at run-time. So while the memory address is "constant" in the sense that it won't change over time while the program is running, its actual value is not assigned at compile-time.

这篇关于变量的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆