Segmentation fault due to large 2D Array in C

0

The below code is generated by parsed SIZE parameters which in this case anywhere you see '2512555' is a parsed SIZE parameter. The problem I have is the array is far too large and causes a Segmentation Fault.

Is there a way to overcome the Segmentation Fault without changing the SIZE parameter? This parameter can range from 0 - 400,000.

#include <stdio.h>
#include <stdlib.h>

double templ25_mem1[2512555][2512555];

int main() 
{
  int templ25_mem1_index1=0;
  int templ25_mem1_index2=0;

    for(templ25_mem1_index1; templ25_mem1_index1 < 2512555; templ25_mem1_index1++)
    {
      for(templ25_mem1_index2; templ25_mem1_index2 < 2512555; templ25_mem1_index2++)
      {
            int rndRow = rand() % 2512555;
            int rndCol = rand() % 2512555;

            templ25_mem1[rndRow][rndCol] = 0x7FFFFFFF;
            templ25_mem1[rndRow][rndCol];    
      }
    }
}
c
arrays
memory
memory-management
asked on Stack Overflow Mar 19, 2018 by Jason

1 Answer

1

To overcome the Segmentation Fault without changing the SIZE parameter, when size can be that large, you need to redesign your data structure(s).

For erxample, you can have this huge array in a temporary file where you swap parts into your main memory. You may also need two-level addressing because you must make sure you can address the array on disk (45TB is larger than the available address space and probably larger than the largest int).

Best could be to go over the requirements again and see if they really require such a large array, or if they allow for other approaches to achieve their goal(s).

answered on Stack Overflow Mar 19, 2018 by Paul Ogilvie

User contributions licensed under CC BY-SA 3.0