为什么书说“编译器为内存中的变量分配空间”? [英] Why do books say, “the compiler allocates space for variables in memory”?

查看:282
本文介绍了为什么书说“编译器为内存中的变量分配空间”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Why do books say, "the compiler allocates space for variables in memory". Isn't it the executable which does that? I mean, for example, if I write the following program,

#include <iostream>
using namespace std;

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

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

and compile it, and get an executable (let it be program.exe), now, if I run program.exe, this executable file will itself command to allocate some space for the variable foo. Won't it ? Please explain why books keep on saying, "the compiler will do this...do that" whereas actually, the compiled executable does that.

添加另一个相关问题到这个问题,为什么 sizeof 称为编译时运算符?

Adding another related question to this question, why is sizeof called a compile-time operator ? Isn't it actually a run-time operator ?

推荐答案

当我们聘请建筑师设计一个房子时,他或她定义房间的大小等,并通知工人(劳动者)。劳动者相应地做工作。但仍然我们会说建筑师这样做房子工人以这种方式做房子。

When we hire an architect to design a house, he or she defines the size of the rooms, etc. and informs the workers (labourers) about it. The labourers do the work accordingly. But still we would say "The architect made the house this way" and not "The labourer made the house this way".

劳动者只是执行架构师定义的步骤。编译器实际上在运行时执行所有用于检查和定义多少内存分配的工作,然后紧跟这些指令。

The labourer is just performing the steps defined by the architect. The compiler actually does all the work for checking and defining how much memory is to be allocated, etc. at run time and then those instructions are just followed.

这篇关于为什么书说“编译器为内存中的变量分配空间”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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