I am trying to read a large data file and passing the data as a 9-dimensional array B[37][24][5][5][8][19][6][19][14]
. I will have to use this array later in the code. But I am getting the Segmentation fault error
.
I have also tried to define B as a pointer, instead of an array and used the command "B = (double *)malloc(5385542400 * sizeof(double));
". But it didn't help.
Here is the code where I defined B as an array.
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <math.h>
#include <string.h>
/* If filename given, write to file; for empty filename write to
screen */
char MYFILE[]="chisq_NHtest_NOvA_nu_app_non-uni_alpha_phi10_10sys.dat";
int main()
{
FILE *outfile = NULL;
outfile = fopen(MYFILE, "w");
if (outfile == NULL)
{
printf("Error opening output file.\n");
return -1;
}
char MYFILE1[]="nova_app_nu_data.dat";
FILE *file1 = NULL;
file1 = fopen(MYFILE1, "r");
if (file1 == NULL)
{
printf("Error reading input file1.\n");
return -1;
}
int i;
static double A[6];
double ret1;
for (i=0; i<6; i++)
{
ret1= fscanf(file1,"%lf ",&A[i]);
if (ret1 == EOF)
{
break;
}
}
char MYFILE2[]="events_vs_E+test_NH_NOvA_nu_app_non-uni_phi10_alpha00_alpha10_alpha11.dat";
FILE *file2 = NULL;
file2 = fopen(MYFILE2, "r");
if (file2 == NULL)
{
printf("Error reading input file2.\n");
return -1;
}
int k,l,m,n,o,p,q,r,s;
double C;
static double B[37][24][5][5][8][19][6][19][14];
double ret2;
for (k=0; k<37; k++)
{
for (l=0; l<24; l++)
{
for (m=0;m<5;m++)
{
for (n=0;n<5;n++)
{
for (p=0; p<8; p++)
{
for (q=0; q<19; q++)
{
for (r=0; r<6; r++)
{
for (s=0; s<19; s++)
{
for (o=0;o<14;o++)
{
fscanf(file2,"%lf ",&ret2);
B[k][l][m][n][p][q][r][s][o]=ret2;
if (ret2 == EOF)
{
break;
}
}
}
}
}
}
}
}
}
}
exit(0);
}
If I change the end value of s loop from 19 to 5, the code is running properly.