main函数中的大量的内存静态分配 [英] Static allocation of huge amounts of memory inside the main function

查看:103
本文介绍了main函数中的大量的内存静态分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有权宣布在C尺寸1000000巨大的整数数组(使用GNU GCC编译器)的程序。我试图声明数组两种不同的方式。

I have a program that has to declare a huge integer array of the size 1000000 in C (using GNU GCC compiler). I tried to declare the array in two different ways.

两个可能的codeS是:

The two possible codes are :

#include <stdio.h>
int arr[1000000];
int main()
{
  return 0; 
}

#include <stdio.h>
int main()
{
  int arr[1000000];
  return 0;
}

后的版本在运行时挂起。什么可能是可能的原因?

The latter version hangs during runtime. What could be the possible reason?

非常感谢!

推荐答案

第二个版本分配在堆栈上,它的大小可以在系统上的限制对于任何给定的过程。在这个过程中的数据段中的第一个分配,其中的大小并不限于(至少对于这些订单分配大小的数量级的)

The second version allocates on the stack, the size of which may be limited on your system for any given process. The first one allocates in the data segment of the process, the size of which isn't limited (at least for these orders of magnitude of allocation size)

从<一个href=\"http://stackoverflow.com/questions/1779545/stack-allocation-limit-for-programs-on-a-linux-32-bit-machine\">this SO回答你可以学习如何检查堆栈分配限制各种平台,如Linux和Windows。如果你是在Linux上它是那样简单:

From this SO answer you can learn how to check for the stack allocation limit for various platforms like Linux and Windows. If you're on Linux it's as simple as:

ulimit -a

这篇关于main函数中的大量的内存静态分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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