2

This has been bothering me for a long time now: Lets say i have a function:

void test(){
    int t1, t2, t3;
    int t4 = 0;
    int bigvar[10000];
    // do something
}

How does the computer handle the memory allocations for the variables?

I've always thought that the variables space is saved in the .exe which the computer will then read, is this correct? But as far as i know, the bigvar array doesnt take 10000 int elements space in the .exe, since its uninitialized. So how does its memory allocation work when i call the function ?

karlphillip
  • 92,053
  • 36
  • 243
  • 426
Newbie
  • 1,143
  • 5
  • 20
  • 30
  • The exe is stored in a _disk_, and compiled code is stored there. But when it is executed, it takes memory from the _processor_ unit. Bytes in the disk do not correspond to bytes needed for variables. – Daniel Daranas Dec 13 '10 at 13:38
  • http://stackoverflow.com/questions/556714/how-does-the-stack-work-in-assembly-language – karlphillip Dec 13 '10 at 13:41

3 Answers3

9

Local variables like those are generally implemented using the processor's stack. That means that the only thing that the compiler needs to do is to compute the size of each variable, and add them together. The total sum is the amount to change the stack pointer with at the entry to the function, and to change back on exit. Each variable is then accessed with its relative offset into that block of memory on the stack.

Your code, when compiled in Linux, ends up looking like this in x86 assembler:

test:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $40016, %esp
        movl    $0, -4(%ebp)
        leave
        ret

In the above, the constant $40016 is the space needed for the four 32-bit ints t1, t2, t3 and t4, while the remaining 40000 bytes account for the 10000-element array bigvar.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

I can't add much to what have already been said, except for a few notes. You can actually put local variables into executable file and have them allocated in the data segment (and initialized) instead of the stack segment. To do that, declare them as static. But then all the invocations of the function would share the same variables while in stack each invocation creates a new set of variables. This can lead to a lot of troubles when the function is called simultaneously by several thread or when there is a recursion (try to imagine that). That's why most languages use stack for local variables and static is rarely used.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
0

On some old compilers, I've met the behavior that the array is statically allocated. Meaning that it sets aside memory for it when it loads the program, and uses that space after that. This behavior is not safe (See Sergey's answer), nor do I expect it to be permitted according to the standards, but I have encountered it in the wild. (I have no memory of what compiler it was.)

For the most part, local variables are kept on the stack, together with return addresses and all that other stuff. This means the uninitialized values may contain sensitive information. This also includes arrays, as per unwind's answer.

Another valid implementation is that the variable found on the stack is a pointer, and that the compiler does the allocation and deallocation (presumably in an exception-safe manner) under the hood. This will conserve stack space (which has to be allocated before the program starts, and cannot easily be extended for x86 architectures) and is also quite useful for C standard VLA (variable length array, aka poor mans std::vector)

MaHuJa
  • 3,148
  • 1
  • 18
  • 6