Search code examples
callocationlarge-data

How to allocate memory for a very large two-dimensional array in C


I have a file which has 5 million rows and 4 columns. As usual, I try to read the data in a 5million by 4 array.

long M = 5000000;
double *coordinates[M];
for (i = 0; i < M; i++){
    coordinates[i] = (double *) calloc(3, sizeof(double));
}

But when I run this code, it has segment fault. After searching the answer online, I know it's because the stack doesn't have so much memory. Someone suggested to allocate memory on the heap with malloc if the array is one-dimensional. But I need a two-dimensional array and I really need so much memory, I hope somebody can help me out of this. Thanks a lot.


Solution

  • You could change

    double *coordinates[M];
    

    to

    double **coordinates = malloc(M * sizeof(*coordinates));
    

    Make sure to free this memory later in the program.

    for (i = 0; i < M; i++){
        free(coordinates[i]);
    }
    free(coordinates);