I would like to read a file with "1 million" rows with 9 columns. The program works well if the file has at the maximum 200,000 rows, otherwise I see the following error :
Segmentation fault : core dumped
error exit code : 139
The problem is that I want to read the file such that I can read values in different columns, therefore, I can not use "fgets" which would scan the whole row. I could not find a good solution to this problem, could I get some help on this ?
Here is a snapshot of my program (omitted the definition of "y" (as seen below)) :
int main(){
int number=290000;
int k,u,i,n = 10000;
float ns_ux[number], ns_uy[number], ns_uz[number], xn[number], yn[number], zn[number];
float l[number],b[number], ns[number], xf,x0,step,s;
FILE *fp=NULL;
fp=fopen("File_new.txt","w");
printf("Enter x0, xf, no. of subintervals: ");
scanf("%f%f%d",&x0, &xf,&n);
FILE* val= NULL;
printf("k xn yn zn int_val tau \n");
val=fopen("file.txt", "r");
for(u=0;u<=(number-1);u++){
fscanf(val,"%f %f %f %f %f %f %f %f %f", &xn[u], &yn[u], &zn[u], &ns_ux[u], &ns_uy[u], &ns_uz[u], &l[u], &b[u], &ns[u]);// \t is tab
}
for(k=0;k<=(number-1);k++){
step = (xf-x0)/n;
s = y(x0,xn[k],yn[k], zn[k], ns_ux[k], ns_uy[k], ns_uz[k], l[k], b[k], ns[k]) + y(xf,xn[k],yn[k], zn[k], ns_ux[k], ns_uy[k], ns_uz[k],l[k], b[k], ns[k]);
for(i = 1; i < n; i++){
s += 2*y(x0+i*step,xn[k],yn[k], zn[k], ns_ux[k], ns_uy[k], ns_uz[k], l[k], b[k], ns[k] );
}
int_val = s*step/2;
fprintf(fp,"%f\t%f\t%1.7f\t%f\n",l[k],b[k],fabs(int_val),ns[k]);
}
fclose(fp);
return 0;
}
int number=290000;
float ns_ux[number], ns_uy[number], ns_uz[number], xn[number], yn[number], zn[number];
Array with 290000
elements on stack may cause problem . You should allocate memory on heap using malloc
.
float *ns_ux;
ns_ux=malloc(sizeof(float)*290000); // like this for all and check its return
Note-
1. You have many arrays , so after allocating free
each one or it may lead to memory leak.
2. Check return of fopen
and fscanf
in your code.
3. For safety initialize s
in your program.
由于这些数组是在堆栈上创建的,因此您可以尝试执行ulimit -s unlimited
以设置“无限”堆栈大小
You should use malloc as it says before and protect it.
float *arr;
arr = (float *)malloc(sizeof(float) * 290000)
if (!arr)
{
printf("error malloc");
exit(0);
}
Don't forget to free !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.