Jin Yan Jin Yan - 6 months ago 30
C Question

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.

Answer Source

You could change

double *coordinates[M];


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

Make sure to free this memory later in the program.

for (i = 0; i < M; i++){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download