我想知道如何更正处理非常大的数组的代码(> 100000000)

时间:2017-01-23 09:13:37

标签: arrays segmentation-fault malloc

通过定义的函数classic_LR时,以下代码存在问题。我认为这是由于将数组'tseries'作为一个因素发送的问题,因为在读取和打印相同的数组时没有问题,尽管它很长。但是,该功能不起作用并给出分段错误。所以我想知道问题是什么,我该如何纠正呢。

P.S。如果我输入更小的数组(< 100000),那么它有时会工作,有时会给出分段错误。

#include <stdio.h>
#include <math.h> /* To use floor function */
#include <stdlib.h>
#include <string.h>
#include "sac_zhu.h"

void classic_LR (int nlen, float ltw, float stw, float thresh, float dt, float *tseries) /* 'tseries' input using separate code(in 'main'), 'nlen' will also be calculated previously (variable length array) */
/* 'ltw', 'stw', 'thresh' are given manually, 'dt' will be calculated from separate code */
{
     int i, j;
     float nl, ns, sra[nlen], sta, lta, sum_lta, sum_sta, pstime, sum=0, mean=0; /* 'pstime' is the variable to be printed as the result of this function */
     float aseries[nlen], dseries[nlen]; /* characteristic function of 'tseries' ('aseries' is the absolute value of 'dseries' for every point and 'dseries' is detrended one for 'tseries' */

     nl=floorf(ltw/dt);
     ns=floorf(stw/dt);

     for(i=0; i<nlen; ++i) /* loop to calculate 'aseries' from 'tseries' */
     {
        sum+=tseries[i];
     }

     mean=sum/(float)nlen;

     for(i=0; i<nlen; ++i) /* loop to calculate 'dseries' from 'tseries' */
     {
         dseries[i]=tseries[i]-mean;
     }

     for(i=0; i<nlen; ++i)
     {
         aseries[i]=abs(dseries[i]);
     }

     for(i=0; i<nlen; ++i) /* initializing sra[] as the zero matrix */
     {
         sra[i]=0;
     }

     for(i=0; i<nlen; ++i)
     {
        if(i>=nl)
        {
           for(j=(i-nl-1); j<i; ++j) /* to calculate LTA */
           {
              sum_lta+=aseries[j];
           }

           lta=sum_lta/nl;

           for(j=(i-1); j<(i+ns); ++j) /* to calculate STA */
           {
              sum_sta+=aseries[j];
           }

           sta=sum_sta/ns;
           sra[i]=sta/lta;
        }

        else
        {
           sra[i]=0;
        }
    }

    for(i=0; i<nlen; ++i)
    {
       if(sra[i]>thresh)
       {
          pstime=i*dt; /* calculating the triggering time */
          printf("%f ",pstime);
       }

       else /* when the triggering does not occur */
       {
          pstime=0;
       }
    }

    printf("\n");

    return;
}

int main(void)
{
    int i, nlen, nerr, n=0;
    float *tseries, *time;
    float ltw, stw, thresh, dt;
    char kname[250]; /* 'kname' is the name of sacfile to be read */

    void classic_LR (int nlen, float ltw, float stw, float thresh, float dt, float *tseries);

    SACHEAD hd;
    /* Copy the name of the file to be read into kname */
    strcpy( kname , "7D.FS01B..BHZ.M.2012.246.235916.SAC" ) ;

    nerr=read_sachead(kname, &hd); /* 'read_sachead' is from 'sac_zhu.h' */
    if (nerr != 0 )
       fprintf(stderr, "Error reading in SAC file: %s", kname);

    time = (float *) malloc(sizeof(float)*nlen); /* dynamic allocation for 'time' array */
    for (n=0; n<nlen; n++)
    {
        time[n] = hd.b+hd.delta*n;
    }
    dt=time[1]-time[0];

    tseries = (float *) malloc(sizeof(float)*nlen); /* dynamic allocation for 'tseries' array */

    nlen = hd.npts; /* total number of observed values i.e. the length of 'tseries' */
    tseries = read_sac(kname, &hd); /* 'read_sac' is from 'sac_zhu.h' */

    if (tseries == NULL )
       fprintf(stderr, "Error reading in SAC file: %s", kname);

    if ( nerr != 0 ) {
       fprintf(stderr, "Error reading in SAC file: %s\n", kname);
       exit ( nerr ) ;
    }

    printf("\n");
    printf("Input the parameters\n");
    printf("ltw: ");
    scanf("%f",&ltw);
    printf("stw: ");
    scanf("%f",&stw);
    printf("thresh: ");
    scanf("%f",&thresh);

    classic_LR(nlen, ltw, stw, thresh, dt ,tseries);

    free(tseries);
    free(time);

    return 0;
}

0 个答案:

没有答案