segmentation fault for 2D arrays

前端 未结 3 1941
慢半拍i
慢半拍i 2020-12-07 06:22

I want to define a 2D array of very big size. But it is giving me segmentation fault?

  #include 

  int main () {
       int i;
       int te         


        
3条回答
  •  再見小時候
    2020-12-07 06:27

    You can allocate the whole table in only one array but you won't be able to access array data with indices using two square brackets:

    int * temp = malloc(4000*5000*sizeof(int));
    

    to access the element (i,j) where previously you wrote temp[i][j], now you should now compute the index the following way:

    temp[i*5000+j];
    

    and do not forget to free the memory allocated for your table afterward:

    free(temp);
    

提交回复
热议问题