0

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;
}
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Cytrix
  • 71
  • 2
  • 5
  • Don't know if they have improved things in the last 10 years, but it used to be you had to increase a compiler setting in Visual Studio to allow programs to use more stack space. – John Shedletsky Apr 16 '15 at 23:24
  • It is C. You have to manage your memory yourself – Norbert Apr 16 '15 at 23:24
  • There are quite a number of possible alternatives to the selected duplicate, but this is one of the earliest questions that is covering the same topic. Search for '[c] stack size limit' to find others -- 75 or so other questions. – Jonathan Leffler Apr 16 '15 at 23:29

1 Answers1

4

The stack has a limited size. Try allocating large arrays on the heap using malloc.

Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69