&是什么QUOT;内存在编译时&QUOT分配;究竟意味着什么? [英] What does "Memory allocated at compile time" really mean?

查看:178
本文介绍了&是什么QUOT;内存在编译时&QUOT分配;究竟意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在像C和C ++编程语言,人们经常提到静态和动态内存分配。我理解的概念,但在编译时所有的内存被分配(保留)一词总是混淆了我。

In programming languages like C and C++, people often refer to static and dynamic memory allocation. I understand the concept but the phrase "All memory was allocated (reserved) during compile time" always confuses me.

编辑,我的理解是,高级别C / C ++ code转换成机器语言,并输出一个可执行文件。如何内存在编译的文件分配?是不是记忆总是与所有虚拟内存管理的东西RAM分配呢?

Compilation, as I understand it, converts high level C/C++ code to machine language and outputs an executable file. How is memory "allocated" in a compiled file ? Isn't memory always allocated in the RAM with all the virtual memory management stuff ?

是不是内存分配定义一个运行时的概念?

Isn't memory allocation by definition a runtime concept ?

如果我做一个1KB静态分配的变量在我的C / C ++ code,将是相同的量增加了可执行文件的大小?

If I make a 1KB statically allocated variable in my C/C++ code, will that increase the size of the executable by the same amount ?

这是那里的短语标题下的静态分配所使用的网页之一。

This is one of the pages where the phrase is used under the heading "Static allocation".

<一个href=\"http://blogs.msdn.com/b/abhinaba/archive/2009/01/16/back-to-basics-memory-allocation-a-walk-down-the-history.aspx\">Back基础:内存分配,下散步记录

推荐答案

在编译时分配的内存意味着编译器在解析特定的地方将事情的过程内存映射的内部分配编译时。

Memory allocated at compile-time means the compiler resolves at compile-time where certain things will be allocated inside the process memory map.

例如,假设有一个全局数组:

For example, consider a global array:

int array[100];

编译器在编译时知道数组的大小和大小的 INT ,因此它知道在编译时数组的大小整体。另外一个全局变量默认有静态存储时间:它是在进程内存空间(的.data / .bss段)的静态存储区域分配。鉴于这些信息,编译器在编译期间在静态存储区的哪个地址阵列将决定

The compiler knows at compile-time the size of the array and the size of an int, so it knows the entire size of the array at compile-time. Also a global variable has static storage duration by default: it is allocated in the static memory area of the process memory space (.data/.bss section). Given that information, the compiler decides during compilation in what address of that static memory area the array will be.

课程,内存地址是虚拟地址。该计划假定它有自己的整个存储空间(从00000000到0xFFFFFFFF为例)。这就是为什么编译器可以做像好吧,阵列将在地址0x00A33211的假设。在运行时的地址由MMU和OS转换为真实/硬件地址。

值初始化静态存储东西是有点不同。例如:

Value initialized static storage things are a bit different. For example:

int array[] = { 1 , 2 , 3 , 4 };

在第一个例子中,编译器才决定在那里阵列将被分配存储在可执行文件的信息。结果
在值初始化的东西的情况下,编译器也注入数组到可执行文件的初始值,并增加了code,它告诉程序加载器,在程序启动数组分配后,数组应该充满这些值。

In our first example, the compiler only decided where the array will be allocated, storing that information in the executable.
In the case of value-initialized things, the compiler also injects the initial value of the array into the executable, and adds code which tells the program loader that after the array allocation at program start, the array should be filled with these values.

下面是由编译器(GCC4.8.1使用x86目标)产生的汇编的两个例子:

Here are two examples of the assembly generated by the compiler (GCC4.8.1 with x86 target):

C ++ code:

C++ code:

int a[4];
int b[] = { 1 , 2 , 3 , 4 };

int main()
{}

输出组件:

a:
    .zero   16
b:
    .long   1
    .long   2
    .long   3
    .long   4
main:
    pushq   %rbp
    movq    %rsp, %rbp
    movl    $0, %eax
    popq    %rbp
    ret

正如你所看到的,值直接注入到组件。在阵列 A ,编译器生成一个零初始化16个字节,因为标准说,静态存储的东西应该是默认初始化为零:

As you can see, the values are directly injected into the assembly. In the array a, the compiler generates a zero initialization of 16 bytes, because the Standard says that static stored things should be initialized to zero by default:

8.5.9(初始值)[注]:结果
  静态存储持续时间的每个对象是零初始化
  任何其他初始化之前,程序启动时发生。在一些
  的情况下,额外的初始化后完成。

8.5.9 (Initializers) [Note]:
Every object of static storage duration is zero-initialized at program startup before any other initial- ization takes place. In some cases, additional initialization is done later.

我总是建议人们拆卸其code,看看真正的编译器与C ++ code一样。这适用于从存储类/持续时间(这样的问题),以先进的编译器优化。你可以指导你的编译器生成的程序集,但也有奇妙的工具,以友好的方式来做到这一点在互联网上。我最喜欢的是 GCC浏览器

I always suggest people to disassembly their code to see what the compiler really does with the C++ code. This applies from storage classes/duration (like this question) to advanced compiler optimizations. You could instruct your compiler to generate the assembly, but there are wonderful tools to do this on the Internet in a friendly manner. My favourite is GCC Explorer.

这篇关于&是什么QUOT;内存在编译时&QUOT分配;究竟意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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