Memory allocation for global and local variables

前端 未结 3 1127
花落未央
花落未央 2020-12-13 16:29

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.

相关标签:
3条回答
  • 2020-12-13 16:41

    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.

    0 讨论(0)
  • 2020-12-13 16:54

    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).

    0 讨论(0)
  • 2020-12-13 17:02

    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.

    0 讨论(0)
提交回复
热议问题