分段错误:11 与 Scanf

Segmentation Fault: 11 with Scanf

我有以下代码:

#include "Analysis.h"
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string.h>
int main(){
    FILE *TS;
//Input Files
    TS = fopen("IceDat2C.dat","r");
//Parametrization
    int i=0,j=0,k=0;;
    double temp,tscale;
    int points = 3606930
    double T[points],A[points],sd[points];
    int n[points];

    fscanf(TS,"%d %lf %lf %lf\n",&n[j],&A[j],&T[j],&sd[j]);
    return 1;
}

程序将 return 段。 fault:11 每次,除非 scanf 函数不存在。 这就是 .dat 文件的样子:带有零的列最终有实数。

 1   0.075   1.79    0
 2   0.075   1.84    0
 3   0.075   1.89    0
 4   0.075   1.84    0
 5   0.075   1.73    0
 6   0.075   1.61    0
 7   0.075   1.49    0
 8   0.075   1.35    0
 9   0.075   1.22    0
 10  0.075   1.07    0
 11  0.075   0.98    0
 12  0.075   0.98    0
 13  0.075   0.97    0
 14  0.075   0.97    0
 15  0.075   0.96    0
 16  0.075   0.94    0
 17  0.075   0.93    0
 18  0.075   0.91    0
 19  0.075   0.89    0
 20  0.075   0.86    0

我不确定我是否理解为什么扫描失败。我使用相同的代码来扫描两列文件,一切顺利。希望大家能帮帮我。

数组 TAsdnmain 函数的局部数组,因此很可能位于堆栈中。其中每一个都有 3606930 个元素,其中 3 个是 double 类型(很可能是 8 个字节),一个是 int 类型(很可能是 4 个字节),所以这些数组占用了超过 100MB 的 space在堆栈上。这对于任何实现来说都太大了,所以你最终会出现堆栈溢出。

对于这种大小的数组,您应该在文件范围内声明它们,以便它们位于数据部分,或者使用 malloc 为它们动态分配内存,以便它们位于堆上。