好奇的分段错误

时间:2014-06-12 09:11:22

标签: c segmentation-fault malloc fgetc

我编写的程序运行良好,没有任何错误。 我读了一个文本文件并将内容写入一个数组(逐行逐行和单行中的seperat字符串)。然后我添加了第二个文本文件并再次尝试阅读内容,但后来我遇到了一些疯狂的分段错误,这些错误仅在我调试程序时报告。

这是导致错误的程序的一部分:

vrFile = fopen("file.txt","r");

//some temporary arrays
    float taz[5000], tel[5000], ttime[5000], tvr[5000][nbins], tdbz[5000][nbins];

    int size = 1024, pos;
    int c;
//buffer for the line to read
    char *buffervr = (char *)malloc(size);
    int lin=0;

    if(vrFile) {
      do { // read all lines in file
        pos = 0;
        do{ // read one line
            c = fgetc(vrFile);
          if(c != EOF) buffervr[pos++] = (char)c;
          if(pos >= size - 1) { // increase buffer length if line is too long - leave room for 0
            size *=2;
            buffervr = (char*)realloc(buffervr, size);
          }
        }while(c != EOF && c != '\n');
        buffervr[pos] = 0;
        // line is now in buffer


        char *ptr;
        ptr = strtok(buffervr,"\t");
        int abs=1;
        while(ptr != NULL) {
            if(abs==1){
                taz[lin] = atof(ptr);
            }
            else if(abs==2) {
                tel[lin] = atof(ptr);
            }
            else if (abs==3) {
                ttime[lin] = atof(ptr);
            }
            else {
                tvr[lin][abs-4]=atof(ptr);
            }
            abs++;

            ptr = strtok(NULL, "\t");

        }
            lin++;
      } while(c != EOF);
      fclose(vrFile);
    }
    //free(buffervr);

首先,当我尝试分配缓冲区char *buffervr = (char *)malloc(size);时,我遇到了分段错误。当我在分配数组float taz[5000], tel[5000], ttime[5000], tvr[5000][nbins], tdbz[5000][nbins];之前执行此操作时,它有效吗?!

第二件事是我无法用c = fgetc(vrFile);读取文件,尽管我可以打开它。在这里我得到另一个分段错误?!

有人能说我出错了吗?

谢谢!

编辑:实际代码:

int size = 1024, pos;
    int c, cdbz;
    char *buffervr = (char *)malloc(size);
    char *bufferdbz = (char *)malloc(size);
    int lin=0;
    char *taz, *tel, *ttime, **tvr, **tdbz;
    taz=(char *) malloc(5000*sizeof(char));
    tel=(char *) malloc(5000*sizeof(char));
    ttime=(char *) malloc(5000*sizeof(char));

    tvr = malloc(5000 * sizeof(char *));
    int i;
    for(i = 0; i < 5000; i++) {
    tvr[i] = malloc(nbins * sizeof(char));
    }

    tdbz = malloc(5000 * sizeof(char *));
    for(i = 0; i < 5000; i++) {
    tdbz[i] = malloc(nbins * sizeof(char));
    }


    //float taz[5000], tel[5000], ttime[5000], tvr[5000][nbins], tdbz[5000][nbins];
    if(vrFile!=NULL) {
      do { // read all lines in file
        pos = 0;
        do{ // read one line
            c = fgetc(vrFile);
          if(c != EOF) buffervr[pos++] = (char)c;
          if(pos >= size - 1) { // increase buffer length - leave room for 0
            size *=2;
            buffervr = (char*)realloc(buffervr, size);
          }
        }while(c != EOF && c != '\n');
        buffervr[pos] = 0;
        // line is now in buffer

        char *ptr;
        ptr = strtok(buffervr,"\t");
        int abs=1;
        while(ptr != NULL) {
            if(abs==1){
                taz[lin] = (ptr);
            }
            else if(abs==2) {
                tel[lin] = (ptr);
            }
            else if (abs==3) {
                ttime[lin] = (ptr);
            }
            else {
                tvr[lin][abs-4]=(ptr);
            }
            abs++;

            ptr = strtok(NULL, "\t");

        }
            lin++;
      } while(c != EOF);
      fclose(vrFile);
    }
    free(buffervr);


.
.
.


int lins,abss;
    for (lins=0; lins<lin; lins++)
    {
        time[0]=ttime[lins];
        az[0]=taz[lins];
        el[0]=tel[lins];
        for (abss=0; abss<nbins; abss++)
        {
            vr[abss]=tvr[lins][abss];
            //dbZ[abss]=tdbz[lins][abss];
        }

    }

注意:time,lat,lon,az,el和vr是指针。

编辑:我用这种方式解决了我的问题:

vrFile = fopen(“file.txt”,“r”);

if (vrFile != NULL){
    while ((getline(&line, &len, vrFile)) != -1) {
        NumberOfLines++;
        if(NumberOfLines == 1){
            line = strtok(line, "\t");
            while(line != NULL){
                NumberOfDoubles++;
                line = strtok(NULL, "\t");
            }
        }
    }
}

rewind(vrFile);

int i,j;

float*  taz;
float*  tel;
float*  ttime;
float** tvr;
float** tdbz;

//1D Arrays
taz   = (float*) malloc (sizeof(float)*NumberOfLines);
tel   = (float*) malloc (sizeof(float)*NumberOfLines);
ttime = (float*) malloc (sizeof(float)*NumberOfLines);

//2D Arrays
tvr   = (float**) malloc (sizeof(float*)*NumberOfLines);
for(i=0;i<NumberOfLines;i++){
    tvr[i]   = (float*) malloc (sizeof(float)*NumberOfDoubles);
}
tdbz   = (float**) malloc (sizeof(float*)*NumberOfLines);
for(i=0;i<NumberOfLines;i++){
    tdbz[i]   = (float*) malloc (sizeof(float)*NumberOfDoubles);
}


if (vrFile != NULL){
    i = 0;
    while ((getline(&line, &len, vrFile)) != -1) {
        line = strtok(line, "\t");
        j = -3;
        while(line != NULL){
            switch(j){
                case -3: taz[i]    = strtod(line, NULL);break;
                case -2: tel[i]    = strtod(line, NULL);break;
                case -1: ttime[i]  = strtod(line, NULL);break;
                default: tvr[i][j] = strtod(line, NULL);break;

            }
            line = strtok(NULL, "\t");
            j++;
        }
        i++;
    }
}

0 个答案:

没有答案