Dealing with large arrays without getting runtime error

后端 未结 1 470
南方客
南方客 2021-01-26 12:25

I am getting runtime error when I used array of size 10^5*10^5 (ex. int a[100000][100000]. As this array is consuming more memory, this may be one one of the reason

相关标签:
1条回答
  • 2021-01-26 13:07

    Three ways to declare the large array int a[100000][100000] are:

    1. Make the large array global
    2. Make the large array static:

      static int a[100000][100000];
      
    3. Use malloc/calloc and dynamically allocate the large array:

      int **a;
      a=malloc(sizeof(int*)*100000);
      for(int i=0;i<100000;i++)
          a[i]=malloc(sizeof(int)*100000);
      
      /*Use the array*/
      
      for(int i=0;i<100000;i++)
          free(a[i]);
      free(a);
      
    0 讨论(0)
提交回复
热议问题