TwiggedToday
TwiggedToday

Reputation: 10155

When is memory allocated during compilation?

When I write

int main()
{
    int j;
}

The memory for j is allocated at the time of compilation, but when during compilation?

What are the various stages of compilation when memory is allotted to a variable?

What if j were global?

Upvotes: 3

Views: 7722

Answers (8)

Joakim Elofsson
Joakim Elofsson

Reputation: 38314

Not at the time of compilation, your 'int j' will get allocated at application startup, when the application enter main() scope (actually it will not technically get allocated, as the stack is being used), globals will get allocated at runtime before entering main() scope.

Upvotes: 5

Thorarin
Thorarin

Reputation: 48516

The compilation process doesn't allocate the memory. It generates the code that allocates the memory :)

In this case j would be a so-called stack variable and it would be allocated when execution enters the main() function. Global and static variables are allocated on the heap instead.

Here's a short explanation: http://www.costech.or.tz/cs231/websites/C%20Programming/www-ee.eng.hawaii.edu/Courses/ee150/Book/chap14/subsection2.1.1.8.html. I'll see if I can find a better one.

Upvotes: 3

Doug
Doug

Reputation: 1093

In C, main is compiled the same as every other function: any variables declared in main will be "allocated" on the stack. A stack frame is the portion of the stack that is used by a single function call. The frame contains slots for all of the locals used within a function. This memory is considered temporary since when the function returns, this frame will be popped off the stack.

The C compiler will assign a static address to global variables. This address is considered part of the binary's "image" and as such has a static location in memory. The C compiler knows the size of every type, so it can set aside the appropriate amount of space in the memory layout of the binary for each global variable. Then, any code that accesses this variable will simply reference this address instead.

You can examine a variable's address with code like this:

#include<stdio.h>

int i;

void foo(int n)
{
    if(n > 2)
        return;

    printf("From foo &n = %xd\n", &n);
    printf("From foo &i = %xd\n", &i);

    foo(n+1);
}


int main()
{
    printf("&i = %xd\n", &i);
    foo(0);
    return 0;
}

Running this code produces output similar to:

./a.out 
&i = 600934d
From foo &n = 38bc4efcd
From foo &i = 600934d
From foo &n = 38bc4eccd
From foo &i = 600934d
From foo &n = 38bc4e9cd
From foo &i = 600934d

There are two things you should notice here:

  1. The address of i is constant every time it is referenced
  2. The address of n (a variable local to the function foo changes with each call to foo. In fact, it will decrease every time, since the stack grows downward.

Upvotes: 6

user59634
user59634

Reputation:

I think you are looking at the stages of compilation, rather than the memory allocation of 'j'. Since I think so, here is what happens:

Once you feed your source code to the C compiler, the first stage(s) is(are) lexical and semantic analysis in which syntax and the semantics of the source code are analysed for correctness. If an error(s) was found, the compiler reports accordingly and does not go ahead. If no errors were found, it proceeds to the generation of a intermediate representation of the source code, usually after various optimisations. This intermediate representation can be in a native language (native to the OS/architecture, like in C) or a platform independent bytecode (like Python/Java..). The function of the compiler ends here.

Memory allocation happens only when you execute the code. This is the runtime of the program. This comes only after the compilation stage, which probably you wouldn't want to know here. If you want to, please let me know. I shall try and add whatever I know.

HTH.

Upvotes: 0

devdimi
devdimi

Reputation: 2462

The memory isn't allocated at the time of compilation, but at runtime. The compiler just generated machine code that will execute your program, actual allocations happen at runtime. In this case the variable isn't used and there won't emitted any code for it.

Upvotes: 0

anon
anon

Reputation:

Compilation generates the executable code for a program. Program memory is allocated when that executable code is run.

Upvotes: 3

qrdl
qrdl

Reputation: 35008

I guess you are mixing things up.

Compiler doesn't allocate memory for variables - it generates code that allocates memory for variables at runtime. For globals is will be added to program start-up code.

Upvotes: 9

ziggystar
ziggystar

Reputation: 28676

It is up to the compiler where to place j. Usually local variables are placed on the stack and as such for your particular example, the compiler will probably reserve the space on the stack for the duration of the main function. Note that this is different from global variable memory, which may receive its own memory.

Upvotes: 0

Related Questions