包括<stdlib.h>导致分段错误</stdlib.h>

时间:2010-06-28 20:17:58

标签: c segmentation-fault std

我正在编写一个程序来从文本文件中读取信息,我让一切正常。问题是我试图添加功能来计算某些字段的平均值,并且必须将字符串转换为双精度。我注意到atof在某些情况下会起作用但在大多数情况下会返回-1。然后我意识到我没有包含stdlib,所以我补充说,但是现在我只用一次更改就得到了一个分段错误。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

main(int argc, char *argv[]){

    int numParts=10;
    long numTests=49000;

    char filename[] = "sweep_data.txt";
    FILE *fp= fopen(filename,"r");
    FILE *out= fopen("TesterData.csv", "w+");
    FILE *meanf= fopen("meanData.txt", "w+");

    char delims[] = " <>";
    char *result = NULL;
    char line [128];

    char *TestNum=NULL;
    char *TestName=NULL;
    char *TestName2=NULL;
    char *SequencerName=NULL;
    char *lowlim=NULL;
    char *hilim=NULL;
    char *value=NULL;
    char *units=NULL;
    char *DeviceNum=NULL;

    char ValueArray[numTests][10];
    char ***ValuePtr = NULL;
    char InfoArray[numTests][20];
    char ***InfoPtr = NULL;

    double mean[numTests];
    long sum;
    int intResult;
    int count=0;
    int DeviceCount=-1;
    int i,j, m,n,k,a,b, len, mLen;

    /*Allocate Memory for 2D arrays*/
    ValuePtr = malloc(numParts * sizeof *ValuePtr);

    for(i=0; i<numParts;i++){
        ValuePtr[i]=malloc(numTests*sizeof *ValuePtr);


        for(j=0; j<numTests; j++){
            ValuePtr[i][j] = malloc(strlen(ValueArray[j]) +1);
        }
    }

    InfoPtr = malloc(6 * sizeof *InfoPtr);

    if(InfoPtr != NULL){
        for(a=0; a<6;a++){
            InfoPtr[a]=malloc(numTests*sizeof *InfoPtr);

            for(b=0; b<numTests; b++){
                InfoPtr[a][b]= malloc(strlen(InfoArray[b]) +1);
            }
        }
    }

    while(fgets(line, sizeof line, fp) != NULL){
        result = strtok(line, delims);
        TestNum=result;

        intResult = strtol(result, NULL, 10);
        if(intResult ==0){
            if(strcmp(result, "Device:")==0){
                DeviceCount++;
                count=0;
            }
            continue; //if doesn't start with a number go to next line
        }

        result = strtok(NULL, delims);
        TestName= result;
        result = strtok(NULL, delims);
        TestName2= result;
        result = strtok(NULL, delims);
        SequencerName = result;
        lowlim=SequencerName;

        if(atof(SequencerName)>1 || atof(SequencerName)<-1){
            result= strtok(NULL, delims);     
            lowlim=result;
            strcat(TestName, TestName2);
        }
        else
            SequencerName= TestName2;     


        if(strstr(TestName, "%")!=NULL || strcmp(TestName,"PTgen")==0 || strcmp(TestName,"mode")==0){
            units="NA";
        }

        else if(strstr(TestName, "2nd") == NULL && strstr(TestName, "3rd") == NULL){
            result= strtok(NULL, delims);
            units = result;
        }

        result= strtok(NULL, delims);
        value=result;

        if(strstr(TestName, "%")==NULL &&  strcmp(TestName,"PTgen")!=0 && strcmp(TestName,"mode")!=0){
            result=strtok(NULL, delims);
            if(strstr(TestName, "2nd") != NULL || strstr(TestName, "3rd") != NULL)
                units = result;
        }

        result=strtok(NULL, delims);
        hilim=result;
        if(hilim[strlen(hilim)-1]=='\n'){
            hilim[strlen(hilim)-1]='\0';
            hilim[strlen(hilim)-1]='\0';
        }

        if(DeviceCount==0){
            strcpy(InfoPtr[0][count], TestNum);
            strcpy(InfoPtr[1][count], TestName);
            strcpy(InfoPtr[2][count], SequencerName);
            strcpy(InfoPtr[3][count], lowlim);
            strcpy(InfoPtr[4][count], hilim);
            strcpy(InfoPtr[5][count], units);
        }

        strcpy(ValuePtr[DeviceCount][count],value);

        count++;
    }

    for(b=0;b<numTests;b++){
        sum=0;
        for(a=0;a<numParts;a++){
            fprintf(meanf, "%s\n", ValuePtr[a][b]);
            sum=atof(ValuePtr[a][b]);
            fprintf(meanf, "%f\n",sum);
        }
        mean[b]=sum/numParts;
        for(n=0; n<2;n++){
            fprintf(meanf, "%s ", InfoPtr[n][b]);
        }
        fprintf(meanf, "%f\n",mean[b]);
    }
    printf("NumTests: %i\n",count); //number of tests run
    printf("NumParts: %i\n", DeviceCount+1);//number of parts run
    fprintf(out,"Test#, TestName,SeqName,LowLim,UpLim,Units,");

    for(j=1; j<=numParts;j++){
        fprintf(out," Device#%i,", j);
    }
    fprintf(out, "\n");

    for (n = 0; n < numTests; n++) {
        for(a=0;a<6;a++){
            fprintf(out, "%s", InfoPtr[a][n]);

            fprintf(out, ", ");
        }

        for (m =0; m < numParts ; m++) {
            fprintf(out, "%s, ", ValuePtr[m][n]);
        }
        fprintf(out, "\n");
    }
}

我需要转换为double的部分是

for(b=0;b<numTests;b++){
    sum=0;
    for(a=0;a<numParts;a++){
        fprintf(meanf, "%s\n", ValuePtr[a][b]);
        sum+=atof(ValuePtr[a][b]);
        fprintf(meanf, "%f\n",sum);
    }
    mean[b]=sum/numTests;
    for(n=0; n<2;n++){
        fprintf(meanf, "%s ", InfoPtr[n][b]);
    }
    fprintf(meanf, "%f\n",mean[b]);
}

2 个答案:

答案 0 :(得分:2)

以下行似乎不正确。它正在对尚未初始化的东西使用strlen。所以结果将是不确定的:

ValuePtr[i][j] = malloc(strlen(ValueArray[j]) +1);

稍后在调用atof()时使用它很可能不会产生一致的结果。并且根据实际的分配大小,在将数据存储到其中时可能会导致seg错误。

答案 1 :(得分:0)

这并不是与segfault有关,而是:

ValuePtr = malloc(numParts * sizeof **ValuePtr);

  ...

InfoPtr = malloc(6 * sizeof **InfoPtr);