#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Segmentation fault / stack overflow


    Hello,

    I'm getting desperate with this:
    My program needs to read the values (integers and floats) from the several files. First file contains the main values, while the other files contain some additional values used in the calculation. Since the main file contains values which need to be used in calculations, modified and then dumped into output file, I have planned to read the whole first file line-by-line, parsing each line and storing the whole content of the first file into the structure which consists of two-dimensional static arrays. Then, when the whole thing is stored in memory, I would do calculations, fill the results out into another structure with arrays, and at the end dump the content of that structure into the output file. Using code::blocks, I get segmentation fault error when trying to run the program:
    Process returned -1073741571 (0xC00000FD) execution time : 3.466 s

    This 0xC00000FD means stack overflow.

    Trying with gcc under Linux, I similarly get "segmentation fault" output.

    I still haven't found where the problem is. Perhaps my arrays are simply too big? If so, how could I reduce the memory consumption while retaining the same idea?

    Below the complete code.

    Thanks for any help / Regards
    Sinisa

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    #include <windows.h>
    #include <unistd.h>
    #include <errno.h>
    
    #define sim_spectrum_constant 122
    #define real_spectrum_constant 181
    #define num_days 365
    #define Output "C:/Temp/output.txt"
    #define Input_spektrum "C:/Temp/spektrum_simulated_input.txt"
    #define Input_indexes "C:/Temp/needed_wavelength_and_interpolation_indexes.txt"
    #define Input_eqe "C:/Temp/eqe-file.txt"
    
    int main () {
      char line[BUFSIZ];
    // result of file operations
      int result=999;
    // i, j: counters; n: number of simulated spectrums; row: row number
      int i, j, n, row, last_row, index1, index2;
      float Wx, norm_sum, norm_factor;
    
    // defining the spectrum structure
      struct spectrum {
        int year[sim_spectrum_constant][num_days];
        int month[sim_spectrum_constant][num_days];
        int day[sim_spectrum_constant][num_days];
        int hour[sim_spectrum_constant][num_days];
        int minute[sim_spectrum_constant][num_days];
        int second[sim_spectrum_constant][num_days];
        float wavelength[sim_spectrum_constant][num_days];
        float irr_total[sim_spectrum_constant][num_days];
        float irr_direct[sim_spectrum_constant][num_days];
        float irr_diffuse[sim_spectrum_constant][num_days];
      };
    
    // defining the output structure
      struct output {
        int year[real_spectrum_constant][num_days];
        int month[real_spectrum_constant][num_days];
        int day[real_spectrum_constant][num_days];
        int hour[real_spectrum_constant][num_days];
        int minute[real_spectrum_constant][num_days];
        int second[real_spectrum_constant][num_days];
        float wavelength[real_spectrum_constant][num_days];
        float irr_total[real_spectrum_constant][num_days];
      };
    
      struct spectrum pS;
      struct output pO;
    
    // initialization
      for (i=0; i<sim_spectrum_constant; i++) {
        for (j=0; j<num_days; i++) {
          pS.year[i][j] = 0;
          pS.month[i][j] = 0;
          pS.day[i][j] = 0;
          pS.hour[i][j] = 0;
          pS.minute[i][j] = 0;
          pS.second[i][j] = 0;
          pS.wavelength[i][j] = 0.;
          pS.irr_total[i][j] = 0.;
          pS.irr_direct[i][j] = 0.;
          pS.irr_diffuse[i][j] = 0.;
        }
      }
    
      for (i=0; i<real_spectrum_constant; i++) {
        for (j=0; j<num_days; i++) {
          pO.year[i][j] = 0;
          pO.month[i][j] = 0;
          pO.day[i][j] = 0;
          pO.hour[i][j] = 0;
          pO.minute[i][j] = 0;
          pO.second[i][j] = 0;
          pO.wavelength[i][j] = 0.;
          pO.irr_total[i][j] = 0.;
        }
      }
    
    // opening files
      FILE *pOutput = fopen(Output, "w");
      FILE *pInput_spektrum = fopen(Input_spektrum, "r");
      FILE *pInput_indexes = fopen(Input_indexes, "r");
      FILE *pInput_eqe = fopen(Input_eqe, "r");
      if (pOutput == NULL || pInput_spektrum == NULL || pInput_indexes == NULL || pInput_eqe == NULL){
        printf ("****Error opening files! -- %s\nPress <enter> to exit...\n", strerror(errno));
        getchar();
        exit(-1);
      }
    
    // determining how many simulated spectrums are provided in the file + reading spectrums into the array
      row = 0;
      n = 0;
      if (fgets(line, sizeof(line), pInput_spektrum) == NULL) {
        printf ("****Simulated spectrum doesn't contain any entries!! -- %s\nPress <enter> to exit...\n", strerror(errno));
        getchar();
        exit(-1);
      }
    
      while(fgets(line, sizeof(line), pInput_spektrum) != NULL) {
        result=sscanf(line, "%d\t%d\t%d\t%d\t%d\t%d\t%f\t%f\t%f\t%f", &pS.year[row][n], &pS.month[row][n], &pS.day[row][n], &pS.hour[row][n], &pS.minute[row][n], &pS.second[row][n], &pS.wavelength[row][n], &pS.irr_total[row][n], &pS.irr_direct[row][n], &pS.irr_diffuse[row][n]);
        row ++;
        if (row == sim_spectrum_constant) {
          n ++;
          last_row = row;
          row = 0;
        }
      }
    
    // check that the number of rows is a multiple of sim_spectrum_constant
      if (last_row != sim_spectrum_constant) {
        printf ("****Wrong number of rows in the last simulated spectrum!! -- %s\nPress <enter> to exit...\n", strerror(errno));
        getchar();
        exit(-1);
      }
    
      fclose(pInput_spektrum);
    
    // starting calculation for each simulated spectrum, and dumping results into the output file
      if (fgets(line, sizeof(line), pInput_indexes) == NULL) {
        printf ("****File with indexes doesn't contain any entries!! -- %s\nPress <enter> to exit...\n", strerror(errno));
        getchar();
        exit(-1);
      }
    
    // for each spectrum
      for (i=0; i<n; i++) {
    // for each real (needed) wavelength
        norm_sum = 0;
        for (j=0; j<real_spectrum_constant; j++) {
          if (fgets(line, sizeof(line), pInput_indexes) != NULL) {
            result=sscanf(line, "%f\t%d\t%d", &Wx, &index1, &index2);
            pO.year[j][n] = pS.year[index1][n];
            pO.month[j][n] = pS.month[index1][n];
            pO.day[j][n] = pS.month[index1][n];
            pO.hour[j][n] = pS.hour[index1][n];
            pO.minute[j][n] = pS.minute[index1][n];
            pO.second[j][n] = pS.second[index1][n];
            pO.wavelength[j][n] = Wx;
            pO.irr_total[j][n] = pS.irr_total[index1][n] + (Wx - pS.wavelength[index1][n]) * ((pS.irr_total[index2][n] - pS.irr_total[index1][n]) / (pS.wavelength[index2][n] - pS.wavelength[index1][n]));
            norm_sum = norm_sum + pO.irr_total[j][n];
          }
          else {
            printf ("****Number of entries in the index file is lower than expected! -- %s\nPress <enter> to exit...\n", strerror(errno));
            getchar();
            exit(-1);
          }
          norm_factor = 970. * (5. / norm_sum);
          for (j=0; j<real_spectrum_constant; j++) {
            pO.irr_total[j][n] = pO.irr_total[j][n] * norm_factor;
            fprintf (pOutput, "%i\t%i\t%i\t%i\t%i\t%i\t%6.4f\t%6.1f\n", pO.year[j][n], pO.month[j][n], pO.day[j][n], pO.hour[j][n], pO.minute[j][n], pO.second[j][n], pO.wavelength[j][n], pO.irr_total[j][n]);
          }
        }
      }
    
      fclose(pOutput);
    
      printf("\n****End of execution -- please check the output file.\nPress <enter> to exit...\n");
      getchar();
      return 0;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    To move pS and pO off of the stack and into static storage, declare them as static; ie:
    Code:
        static struct spectrum pS;
      static struct output pO;
    That should take care of the stack overflow, though the segfault might be caused by something else. At least you should have gotten rid of one problem.

    BTW, when you use something out-of-the-ordinary like C99, you really should inform us of that fact. Since we may have to try to compile your code in order to figure out your problem, we will need to know such things as what language you're using -- to a professional programmer, C99 is not the same thing as C. Basic common courtesy.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    To move pS and pO off of the stack and into static storage, declare them as static...
    Thank you so much for this. It indeed made a difference -- now I have the most common access violation (0xC0000005), and not the stack overflow anymore! I've been struggling with this for a week, changing compiler options etc, to no avail.

    Now at least the debug mode doesn't kick me out immediately (I still get the segmentation fault but later on, at the end of the first initialization loop). At least the main function now starts to execute, which is already a big progress for me, thanks!!

    UPDATE: Using debugger immediately led me to the next mistake -- in initialization loops I used in both FORs i++ (instead i++ in first FOR, and j++ in the second FOR loop).

    Originally Posted by dwise1_aol
    BTW, when you use something out-of-the-ordinary like C99, you really should inform us of that fact. Since we may have to try to compile your code in order to figure out your problem, we will need to know such things as what language you're using -- to a professional programmer, C99 is not the same thing as C. Basic common courtesy.
    Sorry I'm trying to be as courteous as possible, just I am not much into programming -- I make one C program a year, and I don't know much about C standards, compilers, worked just with gcc under Linux and some free C development kits under Windows (like DevC++ and now code::blocks). My programming style probably looks terrible to you... Still even with my poor programming capabilities I am trying to use them occasionally to help me automate some things -- that's all.

IMN logo majestic logo threadwatch logo seochat tools logo