Thread: Need help - error on program termination

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    Need help - error on program termination

    Hey guys, I'm working on a piece of code that examines a *.stl ASCII file generated by CAD that's a list of vertices for a 3D model. My code plods through the file with fscanf, gets the coordinates of the vertices, and computes a bunch of maxima and minima. The program compiled and ran fine with this open-source compiler that I have (BloodShed DevC++ 4), but when I switched to MSVC++ .NET 2003, I started getting this error after the program was finished running:

    Run-Time Check Failure #2 - Stack around the variable 'normal' was corrupted.
    Additionally, some of the computed values had become obviously wrong.

    The *.stl file is a list of vertices that looks like this:

    solid exth
    facet normal 5.612239e-01 6.904722e-01 -4.563725e-01
    outer loop
    vertex 3.362700e-01 8.505000e-01 9.112400e-02
    vertex 3.457880e-01 8.414510e-01 8.913800e-02
    vertex 3.241640e-01 8.400320e-01 6.039900e-02
    endloop
    endfacet
    facet normal -8.197013e-01 5.350310e-01 2.045279e-01
    outer loop
    vertex 2.414680e-01 8.004080e-01 1.561220e-01
    vertex 2.330830e-01 7.962320e-01 1.334410e-01
    vertex 2.282310e-01 7.818640e-01 1.515810e-01
    endloop
    endfacet
    ...
    facet normal 3.132712e-01 -7.011380e-01 -6.405206e-01
    outer loop
    vertex 1.383960e-01 3.755810e-01 1.190080e-01
    vertex 1.139660e-01 3.675140e-01 1.158900e-01
    vertex 1.133830e-01 3.804040e-01 1.014950e-01
    endloop
    endfacet
    endsolid
    And here's my code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
     //Define variables for file input - the FILE variables designate the input and
     //output files, the xyzs are for coordinates, and the rest are junk data
     //placeholders for fscanf.
     char input_path[261];
     float x1, x2, x3, y1, y2, y3, z1, z2, z3, junk1, junk2, junk3;
     char facet[6], normal[7], outer[6], loop[5], vertex1[7], vertex2[7], vertex3[7],
      endloop[8], endfacet[9];
     char solid[6], exth[5];
     FILE *input, *output;
    
     //Define varibales for data manipulation.
     float x_max, y_max, z_max, x_min, y_min, z_min;
     float x_dim, y_dim, z_dim;
     float d_12, d_23, d_13;
     float d_max, d_min, d_avg;
     float sum;
     int counter;
    
     //Prompts the user to enter the input file path.
     puts("Enter the input file path: ");
     gets(input_path);
    
     //Open input file. Return error message if failed.
     if((input = fopen(input_path, "r")) == NULL)
     {
      fprintf(stderr, "Error opening input file.\n");
      system("PAUSE");
      exit(1);
     }
    
     //Read those first two strings.
     fscanf(input, "%s %s", solid, exth);
    
     //Display a friendly message while the program works.
     puts("Computing, please wait...");
    
     //Read coordinates for first element, store them in xyzs.
     fscanf(input, "%s %s %f %f %f %s %s %s %f %f %f %s %f %f %f %s %f %f %f %s %s",
      facet, normal, &junk1, &junk2, &junk3, outer, loop, vertex1, &x1, &y1, &z1,
      vertex2, &x2, &y2, &z2, vertex3, &x3, &y3, &z3, endloop, endfacet);
    
     //Compute distances.
     d_12 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
     d_23 = sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) + (z2-z3)*(z2-z3));
     d_13 = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) + (z1-z3)*(z1-z3));
    
     //Initialize d_max, d_min, and max and min xyzs.
     d_max = d_12;
     d_min = d_12;
     x_max = x1;
     x_min = x1;
     y_max = y1;
     y_min = y1;
     z_max = z1;
     z_min = z1;
    
     //Find d_max and d_min for the first time.
     if(d_23 > d_max)
      d_max = d_23;
     if(d_13 > d_max)
      d_max = d_13;
    
     if(d_23 < d_min)
      d_min = d_23;
     if(d_13 < d_min)
      d_min = d_13;
    
     //Find max and min xyzs for the first time.
     if(x2 > x_max)
      x_max = x2;
     if(x3 > x_max)
      x_max = x3;
     if(x2 < x_min)
      x_min = x2;
     if(x3 < x_min)
      x_min = x3;
    
     if(y2 > y_max)
      y_max = y2;
     if(y3 > y_max)
      y_max = y3;
     if(y2 < y_min)
      y_min = y2;
     if(y3 < y_min)
      y_min = y3;
    
     if(z2 > z_max)
      z_max = z2;
     if(z3 > z_max)
      z_max = z3;
     if(z2 < z_min)
      z_min = z2;
     if(z3 < z_min)
      z_min = z3;
    
     //Initialize sum.
     sum = d_12 + d_23 + d_13;
    
     //Initialize counter.
     counter = 1;
    
     //After the first element, for which all the variables had to be initialized,
     //this while loop can do all the work.
     while( !feof(input) )
     {
      //Read coordinates for an element and store them in the xyzs.
      fscanf(input, "%s %s %f %f %f %s %s %s %f %f %f %s %f %f %f %s %f %f %f %s %s",
       facet, normal, &junk1, &junk2, &junk3, outer, loop, vertex1, &x1, &y1, &z1,
       vertex2, &x2, &y2, &z2, vertex3, &x3, &y3, &z3, endloop, endfacet);
    
      //Compute distances.
      d_12 = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
      d_23 = sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3) + (z2-z3)*(z2-z3));
      d_13 = sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) + (z1-z3)*(z1-z3));
    
      //Find d_max and d_min.
      if(d_12 > d_max)
       d_max = d_12;
      if(d_23 > d_max)
       d_max = d_23;
      if(d_13 > d_max)
       d_max = d_13;
    
      if(d_12 < d_min)
       d_min = d_12;
      if(d_23 < d_min)
       d_min = d_23;
      if(d_13 < d_min)
       d_min = d_13;
    
      //Find max and min xyzs.
      if(x1 > x_max)
       x_max = x1;
      if(x2 > x_max)
       x_max = x2;
      if(x3 > x_max)
       x_max = x3;
      if(x1 < x_min)
       x_min = x1;
      if(x2 < x_min)
       x_min = x2;
      if(x3 < x_min)
       x_min = x3;
    
      if(y1 > y_max)
       y_max = y1;
      if(y2 > y_max)
       y_max = y2;
      if(y3 > y_max)
       y_max = y3;
      if(y1 < y_min)
       y_min = y1;
      if(y2 < y_min)
       y_min = y2;
      if(y3 < y_min)
       y_min = y3;
    
      if(z1 > z_max)
       z_max = z1;
      if(z2 > z_max)
       z_max = z2;
      if(z3 > z_max)
       z_max = z3;
      if(z1 < z_min)
       z_min = z1;
      if(z2 < z_min)
       z_min = z2;
      if(z3 < z_min)
       z_min = z3;
    
      //Revise sum.
      sum += d_12 + d_23 + d_13;
    
      //Increment counter.
      counter++;
     }
    
     //Close input file.
     fclose(input);
    
     //Calculate average distance.
     d_avg = sum / counter;
    
     //Calculate x_dim, y_dim, and z_dim.
     x_dim = x_max - x_min;
     y_dim = y_max - y_min;
     z_dim = z_max - z_min;
    
     //Output information to the output file.
     printf("\nINFORMATION FOR FILE  %s\n", input_path);
     printf("---\n\n");
     printf("There are %d elements.\n\n", counter);
     printf("The maximum distance is  %f .\n", d_max);
     printf("The minimum distance is  %f .\n\n", d_min);
     printf("The average distance is  %f .\n\n", d_avg);
     printf("The x-dimension size is  %f .\n", x_dim);
     printf("The y-dimension size is  %f .\n", y_dim);
     printf("The z-dimension size is  %f .\n\n", z_dim);
     printf("---\n");
     printf("END\n\n");
    
     system("PAUSE");
     return 0;
    }
    Now, I acknowledge that my use of fscanf is lazy, inefficient, and clumsy, but I'm fairly certain it shouldn't be causing these problems that I'm having.

    Anyone have any ideas for a fix?

    (EDITED so as not to break the forum form.)
    Last edited by RaySun; 09-20-2004 at 09:13 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <math.h>
    [edit]Avoid gets.
    [edit]while( !feof(input) ) // why this is bad
    Last edited by Dave_Sinkula; 09-20-2004 at 10:14 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    Dave - adding the math.h header file fixed the output values. Thanks. Why do you suppose it is that the free compiler didn't have this problem, but MSVC .NET did?

    (EDITED - Sorry, didn't realize that those were links. )
    Last edited by RaySun; 09-20-2004 at 12:05 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by RaySun
    Dave - adding the math.h header file fixed the output values. Thanks. Why do you suppose it is that the free compiler didn't have this problem, but MSVC .NET did?
    Different compiler, different problems.

    Quote Originally Posted by RaySun
    Questions about your other suggestions - why is gets() bad? I used it instead of scanf() only to save memory overhead, so I could easily change it, but how could gets() as opposed to scanf() be the cause of my error message?
    I take it you didn't actually read the link they provided you on why gets is bad?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Dev-C++ doesn't provide the advanced buffer overflow checking that is provided by MSVC.NET (or if it does, it is turned off by default). RTC is only turned on when you are compiling a debug version, so if you want, you can compile a release version with the bug intact.

    Attached is a Microsoft write-up on RTC, with examples.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    Thanks for all your help, guys.

    I replaced
    Code:
    while( !feof(input) )
    with
    Code:
    while ( fgetc(input) != EOF )
    but I got the same error message on program termination. Anyone know why this could be happening?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by RaySun
    I replaced
    Code:
    while( !feof(input) )
    with
    Code:
    while ( fgetc(input) != EOF )
    but I got the same error message on program termination. Anyone know why this could be happening?
    How about this?
    Code:
     while( fscanf(input, "%s %s %f %f %f %s %s %s %f %f %f %s %f %f %f %s %f %f %f %s %s",
       facet, normal, &junk1, &junk2, &junk3, outer, loop, vertex1, &x1, &y1, &z1,
       vertex2, &x2, &y2, &z2, vertex3, &x3, &y3, &z3, endloop, endfacet) == 21 )
    Enter the input file path:
    file.txt
    Computing, please wait...

    INFORMATION FOR FILE file.txt
    ---

    There are 3 elements.

    The maximum distance is 0.035994 .
    The minimum distance is 0.013282 .

    The average distance is 0.077165 .

    The x-dimension size is 0.232405 .
    The y-dimension size is 0.482986 .
    The z-dimension size is 0.095723 .

    ---
    END

    Press any key to continue . . .
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Abnormal program termination
    By Kayoss in forum C++ Programming
    Replies: 3
    Last Post: 05-16-2006, 03:29 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. quick termination of program
    By jobolikescake in forum C Programming
    Replies: 7
    Last Post: 01-20-2002, 10:59 PM
  4. abnormal program termination
    By ProLin in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2002, 09:56 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM