I met an error, Segmentation fault. I want to know why this error occurred to me.
Question: Can this error occurred from reckless computation that my computer cannot handle?
When I used the matrix with structure (4094, 30, 50), I met the Segmentation fault. But, When I used the matrix (4094, 4, 50) did not show any error at all.
There is a detailed story below.
I made a C program to read and write a three-dimensional text file processed from Fortran90 program.
The text file that I want to read is written from the Fortran90 program below:
DO X= 1, 4094
DO Y= 1, 30
WRITE(24,*) (MATRIX(X,Y,Z), Z=1,50)
END DO
END DO
So, I thought I wrote a three-dimensional matrix with this structure, X x Y x Z (4094 x 30 x 50), in a text file.
I checked its structure with bash code: wc -l.
wc -l [filename].txt
149220
This means that the number of values in X x Y in the text file. I thought my Fortran90 program wrote the matrix with my intention well .
After that, I made a C program that read and write the text file.
#include <stdio.h>
#define x 4094
#define y 5
#define z 30
int main()
{
int i, j, k;
int state_r, state_w;
double signal [z][y][x];
FILE *fp_r;
FILE *fp_w;
fp_r=fopen("/[filename].txt", "r");
fp_w=fopen("/[filename].txt", "w");
if(fp_r==NULL){
puts("fail to open a file to read!");
return -1;
}
if(fp_w==NULL){
ptus("fail to open a file to write!");
return -1;
}
//read
for(k=0;k<z;k++){
for(j=0;j<z;j++){
for(i=0;i<z;i++){
fscanf(fp_r,"%lf",%signal[k][j][i];
}
}
}
//write
for(k=0;k<z;k++){
for(j=0;j<z;j++){
for(i=0;i<z;i++){
fprintf(fp_w,"%lf",%signal[k][j][i];
}
fprintf(fp_w,"\n");
}
}
state_r=fclose(fp_r);
if(state_r !=0){
printf("error occurred while removing stream");
return 1;
}
state_w=fclose(fp_w);
if(state_w !=0){
printf("error occurred while removing stream");
return 1;
}
return 0;
}
If your answer is "no", then, how can I solve this problem?
In fact, I am reading this matrix with the structure (4094, 30, 30) with python3 program. Thus, I cannot understand why C program cannot cope with this structure!