nit*_*ing 3 c programming-languages data-structures
我有一个程序必须在C中声明一个大小为1000000的巨大整数数组(使用GNU GCC编译器).我尝试以两种不同的方式声明数组.
两个可能的代码是:
#include <stdio.h>
int arr[1000000];
int main()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和
#include <stdio.h>
int main()
{
int arr[1000000];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
后一版本在运行时挂起.可能的原因是什么?
非常感谢!!!
第二个版本在堆栈上分配,对于任何给定的进程,其大小可能在您的系统上受到限制.第一个在流程的数据段中分配,其大小不受限制(至少对于这些分配大小的数量级)
从这个SO答案中,您可以了解如何检查各种平台(如Linux和Windows)的堆栈分配限制.如果您使用的是Linux,它就像下面这样简单:
ulimit -a
Run Code Online (Sandbox Code Playgroud)