cfileheap-memorystack-memoryfclose

How to debug why my C code causes a segmentation fault?


The code below takes a window of size windowSize and shift the window by some shiftSize samples in each iteration.

I did the unusual "printf()" debugging and got that the code is giving error at segmentation fault. Can somebody tell me what the error is?

Code:

 #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;
}

Solution

  • 1) You check feof() first, then do a fscanf(), and then don't check its return value (or re-check feof() at the very least. (You might have been right at the end of the file prior to the fscanf() call, or the configuration file might be malformed, but you wouldn't detect this with your code.)

    2) Your index range checks (and assorted realloc()s) look dodgy. But there is absolutely no chance I'll be doing a runtime analysis of your code, especially since I don't have an input file example to go with.

    Do some Machete Debugging...

    Edit: After joop's comment pointed me toward the fine print of your realloc() (and the if statement around it), in absence of a comment explaining how exactly you expect this to work out, I'll say that you are invoking undefined behaviour there.