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;
}
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.
Adding another related question to this question, why is sizeof
called a compile-time operator ? Isn't it actually a run-time operator ?
It's just a loose use of terminology. Of course the compiler doesn't allocate memory for the program. A more accurate description is that it tells the runtime how much memory to allocate when the program is running.
Until the program is actually run, it isn't in memory (unless it's loaded dynamically, but even that happens during run-time, so out of the scope of the compiler), so there's no memory to speak of.
What those books are talking about is allocating variables whose size is known at compile-time, as opposed to dynamic allocation
cin >> x; int * y = new[x];
, where the size isn't known.It says the compiler allocates space for variables in memory, because otherwise you need to allocate (and free!) memory yourself with
new/malloc
etc.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.
Technically the act of creating the space itself is done at run time, however the compiler is the one to figure out how much space to reserve on the stack in your case, for your
foo
variable.The compiler knows the size of the
int
type and therefore can generate the right assembler instruction that will reserve enough space on the stack in order to letfoo
live there.If you look at the below generated assembler (using MSVC2012) for the program you showed, I have commented some of it to show you what happens:
Of course compiler doesn't "allocate space for variables". Compiler generates a code which allocates space for variables in memory.
I.e. if you have
in source code, compiler may generate a code like
In the x86 architecture, usually that
allocate
thing will be a single assembly instruction:If you have more than one local variable, compiler will pack them into a structure and allocate them together:
will be compiled as
or
Suggest you to read compiler construction. Focus on the storage phase, your query would be resolved.
After the program gets compiled its converted to object code, which is an assembly language code. every line of a high level language program gets translated to many assembly language steps. The translated program is put into the assembler which is executed. There is STORAGE assignment phase in comiler construction which translates to Machine Operation table(MOT instructions at assembly level). This is where space allocation for variables/registers is done. There is a register modifier keyword in C++ as well.