Thread: can't debug the source of segmentation fault in my code

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

    can't debug the source of segmentation fault in my code

    Hello,
    I have the following code which basically read a file and extract the atoms coordinates from it. It almost does the thing except at the very last line when I want to get out of while loop it gives the following errors (in gdb):

    1 10
    3 9.980000 0.000000 0.000000
    4 -4.990000 8.649666 0.000000
    5 0.000000 0.000000 17.056818
    6 -4.990000 -8.649666 -12.792614
    7 -2.494750 -7.208199 -7.107576
    8 -4.990249 -5.766300 -1.420833
    9 -4.990000 -8.649666 -4.264204
    10 -2.494750 -7.208199 -15.635985
    11 -4.990249 -5.766300 -9.949242
    12 0.000000 -8.649666 -12.792614
    13 2.495249 -7.208199 -7.107576
    14 -0.000249 -5.766300 -1.420833
    15 0.000000 -8.649666 -4.264204
    Program received signal SIGSEGV, Segmentation fault.
    _IO_vfprintf_internal (s=0x0, format=0x0, ap=0x0)
    at vfprintf.c:1269
    1269 ORIENT;

    I will appreciate some help to figure out where is the problem exactly.
    ----------------------------------------the code------------------------------------------
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    //#include<string.h>
    #include<math.h>
    
    #define PI 2*acos(0)
    
    int main(int argc, char *argv[])
    {
      FILE *ifp=NULL;
      ifp = fopen("input","r");
    
      char c;
      int i=0,i0=0,i1=0,i2=0;
      int lines, TotalAtoms, N, CNum, CaNum, ONum;
      float x[20],y[20],z[20],RealBasis[3][3];
      float CoordC[50][3],CoordCa[50][3],CoordO[50][3];
    
      if(ifp == NULL)
      {
        lines = 0;
      }
      else
      {
        while((c = fgetc(ifp)) != EOF)
        {
          if(c == '\n')
          {
            lines = lines + 1;
    
    //find the total number of atoms and number of each species
           if(lines == 1)
            {
              fscanf(ifp,"%d %d %d",&CNum,&CaNum,&ONum);
            }
            TotalAtoms = CNum+CaNum+ONum;
    
            if(lines == 2)
            {
              fscanf(ifp,"%d",&N);
              printf("%d %d\n",N, TotalAtoms);
            }  
    
    //Writing the real space basis vectors
           if((lines>=3) && (lines<6))
            {
              fscanf(ifp,"%f %f %f",&x[lines],&y[lines],&z[lines]);
              RealBasis[i][0] = N*x[lines];
              RealBasis[i][1] = N*y[lines];
              RealBasis[i][2] = N*z[lines];
              printf("%d %f %f %f\n",lines,RealBasis[i][0],RealBasis[i][1], RealBasis[i][2]);
              i++;
            }
    
    //Writing the Co-ordiantes of each atoms
            if(lines>=6  && lines <(CNum+6)) //C
            {
              fscanf(ifp,"%f %f %f",&x[lines],&y[lines],&z[lines]);
              CoordC[i0][0] = x[lines];
              CoordC[i0][1] = y[lines];
              CoordC[i0][2] = z[lines];
              printf("%d %f %f %f\n",lines,CoordC[i0][0],CoordC[i0][1],CoordC[i0][2]);
              i0++;
            }
    
            if(lines>=(CNum+6)  && lines <(CNum+CaNum+6)) //Ca
            {
              fscanf(ifp,"%f %f %f",&x[lines],&y[lines],&z[lines]);
              CoordCa[i1][0] = x[lines];
              CoordCa[i1][1] = y[lines];
              CoordCa[i1][2] = z[lines];
              printf("%d %f %f %f\n",lines,CoordCa[i1][0],CoordCa[i1][1],CoordCa[i1][2]);
              i1++;
            }
    
            if(lines>=(CNum+CaNum+6)  && lines<(TotalAtoms+6)) //O
            {
              fscanf(ifp,"%f %f %f",&x[lines],&y[lines],&z[lines]);
              CoordO[i2][0] = x[lines];
              CoordO[i2][1] = y[lines];
              CoordO[i2][2] = z[lines];
              printf("%d %f %f %f\n",lines,CoordO[i2][0],CoordO[i2][1],CoordO[i2][2]);
              i2++;
            }
    
            if(lines == (TotalAtoms+6))
            {
              printf("Stop");
              break;
            }
          }
        }
      }
    
      fclose(ifp);
    -----------------------------------------End of Code------------------------------------
    Here is the "inpit" file:


    C Ca O
    2 2 6
    1
    9.980000 0.000000 0.000000
    -4.990000 8.649666 0.000000
    0.000000 0.000000 17.056818
    -4.990000 -8.649666 -12.792614
    -2.494750 -7.208199 -7.107576
    -4.990249 -5.766300 -1.420833
    -4.990000 -8.649666 -4.264204
    -2.494750 -7.208199 -15.635985
    -4.990249 -5.766300 -9.949242
    0.000000 -8.649666 -12.792614
    2.495249 -7.208199 -7.107576
    -0.000249 -5.766300 -1.420833
    0.000000 -8.649666 -4.264204

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Get a backtrace (type "bt" at the gdb prompt) to find out where, in your code, the problem started.

    If it's available for your platform, though, Valgrind might be a better choice for debugging. It can usually catch an error right where it occurs, whereas gdb might only tell you where the error actually manifested itself.

    At the very least, if you're building with gcc, use the -Wall and -O options. -Wall turns on lots of errors, and -O turns on optimization, which can help catch the use of uninitialized variables. You're using “lines” before a value is set, assuming your fopen() call succeeds.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You need to initialize lines = 0. But your output seems to look OK.
    Also it may be helpful if you display some more labels so that you are sure the right things are in the right "lines":
    Code:
    N, TotalAtoms: 1 10
     3 RealBasis   9.980000   0.000000   0.000000
     4 RealBasis  -4.990000   8.649666   0.000000
     5 RealBasis   0.000000   0.000000  17.056818
     6 CoordC     -4.990000  -8.649666 -12.792614
     7 CoordC     -2.494750  -7.208199  -7.107576
     8 CoordCa    -4.990249  -5.766300  -1.420833
     9 CoordCa    -4.990000  -8.649666  -4.264204
    10 CoordO     -2.494750  -7.208199 -15.635985
    11 CoordO     -4.990249  -5.766300  -9.949242
    12 CoordO      0.000000  -8.649666 -12.792614
    13 CoordO      2.495249  -7.208199  -7.107576
    14 CoordO     -0.000249  -5.766300  -1.420833
    15 CoordO      0.000000  -8.649666  -4.264204
    Stop
    Otherwise I don't get any segfault.

    You should put parenthesis around #define PI (2*acos(0))
    That way when PI is used in an expression there won't be an issue with the multiply being done at the wrong time, e.g. 180/PI would become 180 / 2 * acos(0) when it should be 180 / (2 * acos(0))
    Last edited by nonoob; 04-28-2011 at 09:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM
  2. remove comments from source code
    By limbo100 in forum C Programming
    Replies: 2
    Last Post: 09-29-2001, 06:25 PM
  3. C source code for int25 or code help
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-26-2001, 02:04 AM
  4. The Windows 95 source code
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-23-2001, 07:42 PM
  5. Anybody have the source code to asteroids?
    By Fool in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-02-2001, 06:21 PM