Hello,

I've been writing a scoring program for a molecular dynamics simulation. It has checked out fine with all compiler warnings enabled in the c99 standard, and gdb can't find any problems with it.

However, I realized that it was not giving the warnings for uninitilized values that I was used to in Perl. I found some bugs that I thought I had corrected. I've put many printfs and confirmed that the data is being read in as I intend it to.

A small segment of this 1544 line program, which prints out many errors:

Code:
 const double RAD = 3.14159/180;
 FILE *faz;
 faz = fopen("dmpshifts.ans","r"); //open file dmpshifts.ans for reading
 double p2[29799], p3[29799], p4[29799]; //create 3 error arrays
 if (faz != NULL) { //only go through this if the file exists
    double A[5183], Z[5183], GIAO[5183]; //arrays for 1st,2nd, and 3rd columns
    unsigned int C = 0; //index for A, Z, and GIAO arrays
    double PERR = 1.0; //error to be used later
    while (!feof(faz)) {
       if (fscanf(faz,"%lf %lf %lf\n",&A[C],&Z[C],&GIAO[C]) != 3) {
          continue;
       }
       C++; //increase index for next step
    }
    fclose(faz); //close file handle
    /*------------------*/
    FILE *fz1, *fa2;
    fz1 = fopen("zeta1.99X","r");
    fa2 = fopen("alpha2.99X","r");
    if ((fz1 != NULL) && (fa2 != NULL)) {
       double z[30000], a[30000];
       char x; //disposable variable
       unsigned int c = 0;
       while (!feof(fz1)) {
          if (fscanf(fz1,"%s %lf",&x,&z[c]) != 2) { //first column not stored
             continue;
          }
          if (z[c] < 0) {
             z[c] += 360;
          }
          c++;
       }
       fclose(fz1);
       c = 0;
       while (!feof(fa2)) {
          if (fscanf(fa2,"%s %lf",&x,&a[c]) != 2) {
             continue;
          }
          if (a[c] < 0) {
             a[c] += 360;
          }
          c++;
       }
       fclose(fa2);
       unsigned int j;
       for (j = 200; j <= 30000; j++) {
          double xsumz = 0.0, ysumz = 0.0, xsuma = 0.0, ysuma = 0.0;
          unsigned int k = j-200, m;
          for (m = k; m <= j; m++) {
             xsumz += cos(z[m]*RAD);
             ysumz += sin(z[m]*RAD);
             xsuma += cos(a[m]*RAD);
             ysuma += sin(a[m]*RAD);
          }
          double sumz = (180/3.14159)*(atan2(ysumz,xsumz));
          double suma = (180/3.14159)*(atan2(ysuma,xsuma));
          if (sumz < 0) {
             sumz += 360;
          }
          if (suma < 0) {
             suma += 360;
          }
          if (sumz > 358.72) {
             sumz = 0;
          }
          if (suma > 358.72) {
             suma = 0;
          }
          unsigned int n, q = 0;
          for (n = 0; n <= 5183; n++) {
             if (
                 (fabs(sumz-Z[n]) <= 2.5) && (fabs(suma-A[n]) <= 2.5) && (fabs(GIAO[n]+1.15) < 1)
                ) {
                 score[k]++;
                 q++;
                 break;
             }
          }
          if (q == 1) {
             p2[k] = 0;
          } else {
             p2[k] = 1;
          }
       }
    } else {
      printf("Could not open Z1/A2\n");
    }
  }
Code:
==7322== 1 errors in context 1 of 125:
==7322== Invalid write of size 4
==7322==    at 0x400CF9: main (score.c:67)
==7322==  Address 0x7fefffe00 is on thread 1's stack
==7322== 
==7322== 
==7322== 1 errors in context 2 of 125:
==7322== Invalid read of size 8
==7322==    at 0x400CEA: main (score.c:65)
==7322==  Address 0x7fefffc70 is on thread 1's stack
==7322== 
==7322== 
==7322== 1 errors in context 3 of 125:
==7322== Invalid write of size 4
==7322==    at 0x400C37: main (score.c:55)
==7322==  Address 0x7fefffdfc is on thread 1's stack
==7322== 
==7322== 
==7322== 1 errors in context 4 of 125:
==7322== Invalid read of size 8
==7322==    at 0x400C28: main (score.c:54)
==7322==  Address 0x7fefffc68 is on thread 1's stack
==7322== 
==7322== 
==7322== 1 errors in context 5 of 125:
==7322== Invalid write of size 4
==7322==    at 0x400B75: main (score.c:44)
==7322==  Address 0x7fefffdfc is on thread 1's stack
The program says this for almost every single variable in the program.

I thought that it might be trying to put a floating point into a char might throw it off, but I changed that to a floating point and it didn't change the problem.

My programming paradigm is to keep my variables as local as possible to prevent these kinds of problems and keep the memory usage as low as possible.

I have made printf tests at the offending lines which show that the program is in fact, doing what I want it to.

Do these errors matter? How can I fix them?

I realize this is a really long post. Please let me know if I was in any way unclear and I appreciate you taking time to read this!

-DC