运行时错误C程序

时间:2016-02-01 08:47:02

标签: c

我正在尝试运行一个涉及大型数据集并进行分析的简单程序。该程序非常简单,编译时没有错误。但是在运行它时就停止工作了。我是一名只有基本C知识的业余程序员。我在这里猜测,由于数据量很大,某种数据类型限制被超出。任何帮助表示赞赏! 这是代码:

#include <stdio.h>
#include<math.h>
int main()
{
    int i,n;
    n=193704;
    // original data set arrays 
    double xcor[193706],ycor[193706],zcor[193706],decibel[193706],frequency[193706],node[193706]; 

    // new data set arrays
    double xcor1[193706], ycor1[193706], zcor1[193706], decibel1[193706], frequency1[193706], node1[193706];

    // equating all values of modified arrays to zero for future comparison

    for(i=0;i<n;i++)
    {
        xcor1[i]=0;
        ycor1[i]=0;
        zcor1[i]=0;
        decibel1[i]=0;
        frequency1[i]=0;
        node1[i]=0;
    }


    //FILE POINTERS DEFINED HERE
    FILE *acdb, *freq, *nodes, *xcord, *ycord, *zcord, *hearfreq, *heardb, *hearx, *heary, *hearz ;
    acdb=(fopen("acousticdb.txt","r"));
    freq=(fopen("frequency.txt","r"));
    nodes=(fopen("node.txt","r"));
    xcord=(fopen("xcor.txt","r"));
    ycord=(fopen("ycor.txt","r"));
    zcord=(fopen("zcor.txt","r"));
    hearfreq=(fopen("hearingrangefrequency.txt","w"));
    heardb=(fopen("hearingrangedecibel.txt","w"));
    hearx=(fopen("hearing-x.txt","w"));
    heary=(fopen("hearing-y.txt","w"));
    hearz=(fopen("hearing-z.txt","w"));



    for(i=0;i<n;++i)
    {
        fscanf(acdb, "%lf", &decibel[i]);
        fscanf(freq, "%lf", &frequency[i]);
        fscanf(nodes, "%lf", &node[i]);
        fscanf(xcord, "%lf", &xcor[i]);
        fscanf(ycord, "%lf", &ycor[i]);
        fscanf(zcord, "%lf", &zcor[i]);
    }
    fclose(acdb);
    fclose(freq);
    fclose(nodes);
    fclose(xcord);
    fclose(ycord);
    fclose(zcord);

    // checking frequecy within hearing range
    for(i=0;i<n;i++)
    {
        if(frequency[i]<20000 && frequency[i]>20 )
        {
            xcor[i]=xcor1[i];
            ycor[i]=ycor1[i];
            zcor[i]=zcor1[i];
            decibel[i]=decibel1[i];
            frequency[i]=frequency1[i];
            node[i]=node1[i];
        }
    }

    //wiriting values in text file
    for(i=0;i<n;i++)
    {
        fprintf(hearfreq," %lf \n", frequency1[i]);
        fprintf(heardb," %lf \n", decibel1[i]);
        fprintf(hearx," %lf \n", xcor1[i]);
        fprintf(heary," %lf \n", ycor1[i]);
        fprintf(hearz," %lf \n", zcor1[i]);
    }

    return 0;
}

2 个答案:

答案 0 :(得分:0)

你可能用完了堆栈空间。但是,如果我查看代码,则无需先将所有数据读入数组:所有计算都在一个索引(int main() { int i, n=193704; // original data set arrays double xcor,ycor,zcor,decibel,frequency,node; // new data set arrays double xcor1, ycor1, zcor1, decibel1, frequency1, node1; //FILE POINTERS DEFINED HERE FILE *acdb, *freq, *nodes, *xcord, *ycord, *zcord, *hearfreq, *heardb, *hearx, *heary, *hearz ; acdb=(fopen("acousticdb.txt","r")); freq=(fopen("frequency.txt","r")); nodes=(fopen("node.txt","r")); xcord=(fopen("xcor.txt","r")); ycord=(fopen("ycor.txt","r")); zcord=(fopen("zcor.txt","r")); hearfreq=(fopen("hearingrangefrequency.txt","w")); heardb=(fopen("hearingrangedecibel.txt","w")); hearx=(fopen("hearing-x.txt","w")); heary=(fopen("hearing-y.txt","w")); hearz=(fopen("hearing-z.txt","w")); for(i=0;i<n;++i) { fscanf(acdb, "%lf", &decibel); fscanf(freq, "%lf", &frequency); fscanf(nodes, "%lf", &node); fscanf(xcord, "%lf", &xcor); fscanf(ycord, "%lf", &ycor); fscanf(zcord, "%lf", &zcor); // checking frequecy within hearing range if(frequency<20000 && frequency>20 ) { xcor=xcor1; ycor=ycor1; zcor=zcor1; decibel=decibel1; frequency=frequency1; node=node1; } else xcor1= ycor1= zcor1= decibel1= frequency1= node1= 0.0; //wiriting values in text file fprintf(hearfreq," %lf \n", frequency1); fprintf(heardb," %lf \n", decibel1); fprintf(hearx," %lf \n", xcor1); fprintf(heary," %lf \n", ycor1); fprintf(hearz," %lf \n", zcor1); } fclose(acdb); fclose(freq); fclose(nodes); fclose(xcord); fclose(ycord); fclose(zcord); // don't forget to close these fclose(hearfreq); fclose(heardb); fclose(hearx); fclose(heary); fclose(hearz); return 0; } )上,因此您只需从每个文件中读取一个数据项,检查是否它们在听力频率范围内并写出来:

min(bartar_date)... 
from   
group by bartar_patientname

答案 1 :(得分:0)

如果你声明像这样的数组

double xcor1[193706];

没有使用malloc()或类似的动态内存分配,您将在堆栈上保留该空间。考虑到你在堆栈上做了超过100万个双元素,它可能会溢出。你有一个文字堆栈溢出。

除了Quips之外,为这样的大数组使用动态内存分配。 它的工作原理如下:

double* xcor1 = malloc(sizeof(double) * 193706) ;

当你完成所有这些时,也不要忘记释放它。

free(xcor1);

当然,使用您的代码,您也可以不使用所有数组而不保存数据,而是使用单个变量进行操作。