数组大小导致C崩溃

时间:2015-04-16 23:22:36

标签: c arrays

我正在努力在C中创建大量的结构。我需要数组来容纳超过100万个结构实例。但是,当阵列大小超过几十万时程序崩溃。它运行良好的第一位然后它崩溃。我正在运行带有6 GB RAM的Windows 7。

这个问题的根本原因是什么?以下是代码

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

1 个答案:

答案 0 :(得分:4)

堆栈的大小有限。尝试使用malloc在堆上分配大型数组。