如何调试我的C代码导致分段错误的原因?

时间:2014-09-05 14:21:08

标签: c file stack heap fclose

下面的代码采用一个大小为windowSize的窗口,并在每次迭代中将窗口移动一些shiftSize样本。

我做了不寻常的“printf()”调试,并得出代码在分段错误时给出错误。有人能告诉我错误是什么吗?

代码:

 #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <tgmath.h>
    int main ()
    {

        FILE *fp, *in   ;

        in = fopen ("controlFile.txt", "r");

        if (in == NULL) {
          fprintf(stderr, "Can't open input file in.list!\n");
          exit(1);
        }

        char equalTo, commandType[20];
        int commands[3]; int i=0;

        while (!feof(in)){

                fscanf(in, "%s %c %d\n", commandType, &equalTo, &commands[i]);
                printf("%s %c %d\n", commandType, equalTo, commands[i]);
                i++;
        }

        fclose(in);

        fp = fopen ("OriginalData.txt", "r");

        if (fp == NULL) {
          fprintf(stderr, "Can't open input file in.list!\n");
          exit(1);
        }

    //Note: time is milliseconds. Therefore, multiplying factor is 1000
        int mulFactor =1000;

        int samplesPerSecond = commands[0];

        int windowSize = floor((commands[1]*mulFactor)/samplesPerSecond); //This will be our array size or rank for cuda Program

        int shiftSize = floor ((commands[2]*mulFactor)/samplesPerSecond);

        int fileCounter  = 0, breakFlag=0;
        int allocationSize = 100;
        float *values, test;

        values = (float*) malloc (100*sizeof(float));

        if (values==NULL)
                {
                        printf("Error allocating memory!");
                        exit (1);
                }
        int localCounter = 0;
        int arrayCounter = 0;
        int copyCounter = windowSize - shiftSize;
//      printf("SamplesPerSecond: %d\n windowSize: %d\n shiftSize: %d\n copyCounter: %d\n", samplesPerSecond, windowSize, shiftSize, copyCounter);
        int temp;
        float* check;
        while (!feof(fp)){
                localCounter = 0;
                if (fileCounter==0){
                        while (!feof (fp) && localCounter!=windowSize){
                                fscanf(fp, "%f", &values[arrayCounter]);
                                printf("%f\n", values[arrayCounter]);
                                localCounter++;
                                fileCounter++;
                                                      arrayCounter++;
                                //printf("%f\n", values[arrayCounter]);
                                if (sizeof(values)/sizeof(float)==arrayCounter-1){
                                        values = (float*)realloc (values, (size_t)(allocationSize*sizeof(float)));
                                        if (values==NULL){
                                                printf("Cannot allocate memory\n");
                                                exit(1);
                                        }
                                }
                        }
                }
                else{
                        temp = copyCounter;
                //      printf("Here\n"); 
                        while (temp!=0 && !feof(fp)){
                                  //if (feof(fp)) {printf ("Been Here\n");breakFlag = 1; break;}
                                values[arrayCounter] = values [arrayCounter-copyCounter];
                                printf("%f\n", values[arrayCounter]);
                                temp--;
                                arrayCounter++;
                                localCounter++;
                                if (sizeof(values)/sizeof(float)==arrayCounter-1){
                                        values= (float*)realloc (values, allocationSize*sizeof(float));
                                        if (values==NULL){
                                                printf("Cannot allocate memory\n");
                                                exit(1);
                                        }
                                }

                        }
                         while (localCounter!=windowSize && !feof(fp)){
                                fscanf(fp, "%f", &values[arrayCounter]);
                                printf("%f\n", values[arrayCounter]);
                                localCounter++;
                                fileCounter++;
                                               arrayCounter++;
                                if (sizeof(values)/sizeof(float)==arrayCounter-1){
                                       values= (float*)realloc (values, allocationSize*sizeof(float));
                                        if (values==NULL){
                                                printf("Cannot allocate memory\n");
                                                exit(1);
                                        }
                        }
                        }
                }
        }
        fclose(fp);
        //int numOfFrames = floor((fileCounter-1)/shiftSize);  //Count the number of lines when fp is increasing
        //int j;
/*      for(j=0; j<(sizeof(values)/sizeof(float)); j++){
                printf ("%f\n", values[j]);
        }
*/
        return 0;
}

1 个答案:

答案 0 :(得分:2)

1)您首先检查feof()然后执行fscanf(),然后不检查其返回值(或重新检查至少检查feof()。(在fscanf()调用之前,您可能已经在文件的末尾,或者配置文件可能格式不正确,但您不会在代码。)

2)您的索引范围检查(以及各种realloc() s)看起来很狡猾。但是我绝对没有机会对你的代码进行运行时分析,特别是因为我没有输入文件示例。

做一些Machete Debugging ...

编辑:在joop的评论指出我的realloc()(及其周围的if声明)的细则后,如果没有评论解释你究竟是怎么回事我希望你能解决这个问题。我会说你在那里调用未定义的行为。