数组大小导致C崩溃 [英] Array size causing C to crash
本文介绍了数组大小导致C崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力在C中制作大量的结构体。我需要该数组容纳超过一百万个结构体实例。但是,当数组大小超过数十万时,程序将崩溃。最初运行良好,然后崩溃。我正在运行具有7 GB RAM的Windows 7。
I am working to make a large array of structs in C. I need the array to hold over 1 million struct instances. However when the array size gets above a couple hundred thousand the program crashes. It runs fine for the first bit then it crashes. I am running Windows 7 with 6 gb of RAM.
此问题的根本原因是什么?下面是代码
What is the root cause of this problem? Below is the code
struct Job {
unsigned long id;
unsigned int num_preds;
unsigned int resources;
unsigned int* pred_array;
};
int main()
{
//Counter and loop variables (Do not use for any other purpose)
unsigned int i,j,k,count;
unsigned long height,num_jobs;
// This is our input section
height = 1000;
//Calculate the number of jobs
num_jobs = (height+1)*height*0.5;
printf("%d \n",num_jobs);
struct Job jobs[num_jobs];
return 0;
}
推荐答案
堆栈的大小有限。尝试使用malloc在堆上分配大型数组。
The stack has a limited size. Try allocating large arrays on the heap using malloc.
这篇关于数组大小导致C崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文