11

为什么书上说“编译器为内存中的变量分配空间”。不是可执行文件吗?我的意思是,例如,如果我编写以下程序,

#include <iostream>
using namespace std;

int main()
{
   int foo = 0;
   cout<<foo;
   return 0;
}

并编译它,得到一个可执行文件(让它成为program.exe),现在,如果我运行program.exe,这个可执行文件将自己命令为变量foo分配一些空间。不会吗?请解释为什么书籍一直说,“编译器会做这个......做那个”,而实际上,编译的可执行文件会这样做。

在这个问题中添加另一个相关问题,为什么sizeof称为编译时运算符?它实际上不是运行时运算符吗?

4

7 回答 7

20

当我们聘请建筑师设计房屋时,他或她会定义房间的大小等,并告知工人(劳工)。工人们相应地进行工作。但我们仍然会说“建筑师这样建造房子”而不是工人这样建造房子”。

工人只是在执行建筑师定义的步骤。编译器实际上在运行时完成了检查和定义要分配多少内存等所有工作,然后只遵循这些指令。

于 2013-04-04T08:30:24.797 回答
11

从技术上讲,创建空间本身的行为是在运行时完成的,但是编译器会确定在您的情况下为您的变量在堆栈上保留多少空间。foo

编译器知道int类型的大小,因此可以生成正确的汇编指令,该指令将在堆栈上保留足够的空间以便在foo那里生存。

如果您查看下面为您展示的程序生成的汇编程序(使用 MSVC2012),我已经对其中的一些进行了注释以向您展示发生了什么:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
//Setup stack frame for main by storing the stack pointer from the calling function and 
//reserving space for local variables and storing commonly used registers on the stack
002E4390  push        ebp  
002E4391  mov         ebp,esp  
// reserve space for local variables, which is 204 bytes here, no idea why so much.
// this is where the compiler calculated the size of your foo and added that to whatever else needs to be stored on the stack. Subtract from stack pointer (esp) because stack grows downward.  
002E4393  sub         esp,0CCh  
002E4399  push        ebx  
002E439A  push        esi  
002E439B  push        edi  
002E439C  lea         edi,[ebp-0CCh]  // load effective address of [ebp-0CCh], which I suspect would be your foo variable into edi register
002E43A2  mov         ecx,33h  
002E43A7  mov         eax,0CCCCCCCCh  
002E43AC  rep stos    dword ptr es:[edi]  //fill block of memory at es:[edi] with stuff  
   int foo;
   return 0;
002E43AE  xor         eax,eax  //set eax to zero for return value
}
// restore everything back to how it was before main was called
    002E43B0  pop         edi  
    002E43B1  pop         esi  
    002E43B2  pop         ebx  
    002E43B3  mov         esp,ebp  
    002E43B5  pop         ebp  
    002E43B6  ret  
于 2013-04-04T08:22:29.547 回答
8

这只是术语的松散使用。当然,编译器不会为程序分配内存。更准确的描述是它告诉运行时程序运行时要分配多少内存。

在程序实际运行之前,它不在内存中(除非它是动态加载的,但即使是在运行时发生的,所以超出了编译器的范围),所以没有内存可言。

这些书谈论的是分配大小在编译时已知的变量,而不是大小未知的动态分配cin >> x; int * y = new[x];

于 2013-04-04T08:21:58.510 回答
3

它说编译器为内存中的变量分配空间,因为否则您需要自己分配(并释放!)内存new/malloc等。

于 2013-04-04T08:21:49.323 回答
2

当然编译器不会“为变量分配空间”。编译器生成一个代码,为内存中的变量分配空间。

即如果你有

int foo;
foo = 1;

在源代码中,编译器可能会生成类似的代码

int* fooPtr = allocate sizeof(int)
*fooPtr = 1;

在 x86 架构中,通常那个allocate东西是一条汇编指令:

sub esp, 4    ; allocate 4 == sizeof(int) bytes on stack
              ; now the value of "esp" is equal to the address of "foo",
              ; i.e. it's "fooPtr"
mov [esp], 1  ; *fooPtr = 1

如果你有多个局部变量,编译器会将它们打包成一个结构并一起分配它们:

int foo;
int bar;
bar = 1;

将被编译为

struct Variables { int foo; int bar; };
Variables* v = allocate sizeof(Variables);
v->bar = 1;

或者

sub esp, 4+4       ; allocate sizeof(Variables) on stack
mov [esp + 4], 1   ; where 4 is offsetof(Variables, bar)
于 2013-04-18T11:04:51.683 回答
0

编译器生成机器指令并计算出局部变量将占用的内存地址。每个局部变量都被赋予一个相对于栈顶的地址,例如foo,假设位于内存地址stack_pointer。如果您有一个变量foo2,它将被放置在stack_pointer + 44 是int.

foo访问局部变量时,编译器将替换stack_pointer. 硬件有一个特殊的stack_pointer寄存器,它总是指向当前堆栈的顶部。

编译器知道每个变量的大小,因为它负责查看structclass声明并计算出它在内存中的布局方式。所以sizeof在编译时已知并被视为常量表达式。原始类型 likeint已知具有一定的大小,例如sizeof(int)4。

于 2016-05-13T08:35:33.053 回答
-1

建议你阅读编译器构造。专注于存储阶段,您的查询将得到解决。

在程序被编译后,它转换为目标代码,这是一种汇编语言代码。高级语言程序的每一行都被翻译成许多汇编语言步骤。翻译后的程序被放入执行的汇编程序中。编译器构造中存在存储分配阶段,该阶段转换为机器操作表(汇编级别的 MOT 指令)。这是为变量/寄存器分配空间的地方。C++ 中也有一个寄存器修饰符关键字。

于 2014-03-27T10:06:33.790 回答