Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do books say, “the compiler allocates space for variables in memory”?

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 ?

like image 916
Shiva Avatar asked Apr 04 '13 08:04

Shiva


People also ask

Does compiler allocate memory?

When a variable is declared compiler automatically allocates memory for it. This is known as compile time memory allocation or static memory allocation. Memory can be allocated for data variables after the program begins execution. This mechanism is known as runtime memory allocation or dynamic memory allocation.

How do compiler assign memory addresses to variables?

Typically local variables are put on the "stack". This means that the compiler assigns an offset to the "stack pointer" which can be different depending on the invocation of the current function. I.e. the compiler assumes that memory locations like Stack-Pointer+4, Stack-Pointer+8, etc.

When variable is declared the compiler allocates memory to the variable by calculating the size of the variable *?

The compiler allocates memory for the union by considering the size of largest member. In union, the members share same memory location. As the members of the union share memory only one member enters in the memory at a time and only that member is initialized while other member hold garbage value.

What is memory for variable allocation in C?

3.2. 1 Memory Allocation in C Programs The space is allocated once, when your program is started (part of the exec operation), and is never freed. Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable.


3 Answers

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.

like image 61
Luchian Grigore Avatar answered Oct 21 '22 15:10

Luchian Grigore


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

int foo;
foo = 1;

in source code, compiler may generate a code like

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

In the x86 architecture, usually that allocate thing will be a single assembly instruction:

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

If you have more than one local variable, compiler will pack them into a structure and allocate them together:

int foo;
int bar;
bar = 1;

will be compiled as

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

or

sub esp, 4+4       ; allocate sizeof(Variables) on stack
mov [esp + 4], 1   ; where 4 is offsetof(Variables, bar)
like image 37
Abyx Avatar answered Oct 21 '22 17:10

Abyx


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.

like image 25
Suvarna Pattayil Avatar answered Oct 21 '22 17:10

Suvarna Pattayil