Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory allocation for global and local variables

I have learnt that memory for global variables are allocated at program startup whereas memory for local variables are allocated whenever function call is made.

Case 1:
I have declared a global integer array of size 63500000 and memory used is 256 MB
Ideone Link

include <stdio.h>
int a[63500000];
int main()
{
    printf ("This code requires about 250 MB memory\n");
    return 0;
}

Case 2:
I have declared a local integer array of same size in main() and memory used is 1.6 MB
Ideone link

#include <stdio.h>
int main()
{
    int a[63500000]= {1,5,0};
    printf ("This code requires only 1.6 MB \n");
    //printf ("%d\n", a[0]);
    return 0;
}

Case 3:
I have declared a local integer array of same size in another function and memory used is 1.6 MB
Ideone Link

#include <stdio.h>
void f()
{
    int a[63500000];
}

int main()
{
    f();
    return 0;
}

Please explain why there is difference in memory used or my concept of memory allocation is wrong ??

like image 473
Snehasish Avatar asked Aug 15 '12 21:08

Snehasish


People also ask

How memory is allocated for local and global variables?

Each static or global variable defines one block of space, of a fixed size. 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.

How memory is allocated for local variables?

Solution. The allocation of Local Variables occurs when the calling VI is loaded into memory. If it is a stand-alone VI, then the memory for the Local Variable is allocated at run-time and deallocated at the end of its run.

Where are global and local variables stored in memory?

Global variables are stored in the data segment of memory. Local variables are stored in a stack in memory. We cannot declare many variables with the same name. We can declare various variables with the same name but in other functions.

Do global variables use RAM?

Global variables will most likely always be stored in RAM if you don't do some crazy stuff with your compiler (allocating a register for a variable). But you can't be sure that local variables will not be stored in RAM.


3 Answers

Cases 2, 3

Variables that you define inside functions are allocated on the stack. That means that the associated memory is cleaned up (the stack is "popped") when the function exits.

Case 1

Variables defined in global scope are allocated in a data segment (or, generally, a memory space requested from the operating system) that exists for the lifetime of the process.

Additionally

Memory allocated using malloc is allocated from a heap and remains allocated until explicitly released using free.

Note that a modern OS may well provide address space requested by a program, but not physically back that address space with RAM until the memory (or a portion of the memory often called a page) is physically accessed.

like image 130
Eric J. Avatar answered Nov 03 '22 20:11

Eric J.


First of all: the ideone compiler is GCC.

So, what does GCC do when you compile this?:

void foo ()
{
  int a[63500000];
}

gcc -S -O2 foo.c generates:

foo:
    pushl   %ebp
    movl    %esp, %ebp
    popl    %ebp
    ret

i.e. nothing is allocated on the stack, at all.

The array is simply optimized away by GCC because it is never used.

GCC won't do this with a global, because it is possible that a global is used in another compilation unit, and so it isn't sure that it is never used. Also: The global is not on the stack (since it is a global).

Now, lets see what happens when you actually use the local array:

int bar (int a, int b, int c)
{
  int f[63500000];
  f[a] = 9;
  f[b] = 7;
  return f[c];
}

Things are very different:

bar:
    pushl   %ebp
    movl    %esp, %ebp
    subl    $254000000, %esp
    movl    8(%ebp), %eax
    movl    $9, -254000000(%ebp,%eax,4)
    movl    12(%ebp), %eax
    movl    $7, -254000000(%ebp,%eax,4)
    movl    16(%ebp), %eax
    movl    -254000000(%ebp,%eax,4), %eax
    leave
    ret

This line: subl $254000000, %esp corresponds to the size of the array. i.e. memory is allocated on the stack.

Now, what if I tried to use the bar function in a program:

int bar (int a, int b, int c)
{
  int f[63500000];
  f[a] = 9;
  f[b] = 7;
  return f[c];
}

int main (void)
{
  return bar (0, 0, 0);
}

We already saw, that the bar function allocates 250 or so megabytes on the stack. On my default GNU/Linux install, the stack size is limited to 8MB. So when the program runs, it causes a "Segmentation fault". I can increase it if I want, by executing the following in a shell:

ulimit -s 1000000 #i.e. allow stack size to grow close to 1GB

Then I can run the program, and it will indeed run.

The reason why it fails on the ideone website is that they have limited the stack size when executing programs (and they should, otherwise malicious users could mess up their system).

like image 24
ArjunShankar Avatar answered Nov 03 '22 18:11

ArjunShankar


case 2 and case 3 would result in stack overflow as you are asking for 64 MB of stack memory wherein your stack is typically 8 MB on Linux . this would result in random bad things and /or core dumps and crashes.

this answer greatly explains various sections of process address space (.text, .bss , .data )and how various allocations of variables is done.

like image 22
Jay D Avatar answered Nov 03 '22 18:11

Jay D