全局和局部变量内存分配 [英] Memory allocation for global and local variables

查看:171
本文介绍了全局和局部变量内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到,内存全局变量在程序启动时分配的,而每当函数调用局部变量的内存分配。

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.

案例1:结果
我宣布用大小63500000和存储器的全局整型数组为256 MB结果
Ideone链接

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

案例2:结果
我宣布在主同样大小的地方整型数组()和使用的内存为1.6 MB结果
Ideone链接

#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;
}

案例3:结果
我宣布同样大小的地方整数阵列中的另一个功能,使用的内存为1.6 MB结果
Ideone链接

#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 ??

推荐答案

首先:在ideone编译器GCC是

First of all: the ideone compiler is GCC.

那么,是什么GCC编译时做这?

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

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

GCC -S -O2 foo.c的生成:

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

即。的没有的是在栈上分配的所有

数组是简单地优化掉海湾合作委员会,因为它从来没有使用过。

GCC将不具有全局做到这一点,因为它有可能是一个全球性的是在另一个编译单元中使用,因此它不能确定它是从未使用过。也:全局是未在堆栈上的(因为它是一个全球性的)

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

这行: subl $ 254000000,ESP%对应于数组的大小。即内存的的分配在堆栈中。

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);
}

我们已经看到,该函数分配在栈上250左右兆字节。在我的默认的GNU / Linux的安装,堆栈大小限制为8MB。所以,当程序运行时,它会导致段错误。我可以增加它,如果我想,通过在shell执行以下内容:

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.

为什么它的ideone网站上失败的原因是在执行程序时,他们已经限制了堆栈大小(他们应该,否则,恶意用户可能会搞砸了他的系统)。

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

这篇关于全局和局部变量内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆