You are here:

C/Segmentation defult

Advertisement


Question
Dear Narendra: sorry, I didn't explain it well. I simplified  my code and input file to make them understandable. My problem is a segmentation fault, which occurs when I try to close the file3 in the last line of my program ("fclose(file3)"). If I omit this line ("fclose(file3)") the program works. In addition, I realized that if I delete the following code of my program (NOTE 1), there is no such error anymore.
/** ********* Start NOTE 1 *****************/
         mol[id].box.x   = bx;
                   mol[id].box.y   = by;
                   mol[id].box.z   = bz;
/** ********* End NOTE 1 *****************/
My question is why I'm doing wrong to face this problem. I belive that closing the file3 is not related to this. Or I'm using wrong the "sscanf" command. I checked all the variables and their declarations are consistent. Hope you can help me to figure out my mistakes. Thanks in advance.
/**********My code************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

#define AllocMem(a, n, t)  a = (t *) malloc ((n) * sizeof (t))
#define PI 3.14159265358979323

typedef struct {double x, y, z;} VecR;
typedef struct {int x, y, z;} VecI;

typedef struct {
 VecI box;
 VecR r, rv, ra;
 int type,Nstep;
 long int step;
} Mol;

int main()
{  Mol      *mol;
  int      id, type;
  double    Lx = 1.0, Ly = 1.0, Lz = 1.0;
  int       bx ,by, bz;
  double    xp,yp,zp;
  char    char_TimeStep[80] = " ITEM: TIMESTEP\n";
  char    char_Atoms[80] = " ITEM: ATOMS\n";
  char    string_temp[80] ;
  long int     Nrigth=200, Nleft=100;
  int       counter, counter2,i=1;
  long int    Tempstep=0;
  long int     Nm = 3, Nc =2, natoms=5;
  long int    Model=0;
  char    AtomType[]={'T','R','C','S','\0'};
  FILE    *file1,*file3,*file2;

 if ( ( file1 = fopen( "input", "r" ) ) == NULL ){
     printf( "File xxx could not be created. \n");
  return 0;}

  if ( ( file2 = fopen( "DNA.pdb", "w" ) ) == NULL ){
     printf( "File xxx could not be created. \n" );
  return 0;}

  if ( ( file3 = fopen( "Natoms", "w" ) ) == NULL ){
     printf( "File xxx could not be created. \n");
  return 0;}


 AllocMem (mol, natoms, Mol);

fprintf (file2,"HEADER    pcl\n");
fprintf (file2,"REMARK    THIS IS A SIMULATION BOX\n");

fprintf (file3,"#Static properties \n");
fprintf (file3,"#Timestep (1), Nleft (2), Nrigth (3)\n");

   printf("Number of reading frames = \n");
   fgets(string_temp,80,file1);
  /** get information from  'samplehistorycopy'  file**/
   while (!feof( file1 ) ){

      if (strcmp (string_temp,char_TimeStep) == 0){
     fgets(string_temp,80,file1);   
          sscanf(string_temp,"%ld",&Tempstep);  
          fgets(string_temp,80,file1);
          }
           
      if (strcmp (string_temp,char_Atoms) == 0){
              
              for (counter = 1; counter <= natoms; counter++ )
         {
         fgets(string_temp,80,file1);
                   sscanf(string_temp,"%d%d%lf%lf%lf%d%d%d",&id,&type,&xp,&yp,&zp,&bx,&by,&bz);
         mol[id].type     = type;
                   mol[id].step     = Tempstep;
                   /** getting the real possition **/
                   mol[id].r.x      = xp*Lx + bx*Lx;
                   mol[id].r.y      = yp*Ly + by*Ly;
                   mol[id].r.z      = zp*Lz + bz*Lz;
                   /** ********* Start NOTE 1 *****************/
         mol[id].box.x   = bx;
                   mol[id].box.y   = by;
                   mol[id].box.z   = bz;
                   /** ********* End NOTE 1 *****************/
         }
      /** **** write  into file with fprintf ****/
                   Model++;

                   fprintf (file2,"MODEL   %ld\n",Model);
                   fprintf (file2,"TIME STEP %ld\n", Tempstep);
                   for(counter2=1;counter2<=natoms; counter2++){
                   fprintf (file2,"ATOM   %4.d  %c              %8.2f%8.2f%8.2f\n",counter2,AtomType[type],mol[counter2].r.x, mol[counter2].r.y, mol[counter2].r.z);}
                  fprintf (file2,"ENDMDL\n");

                  fprintf (file3,"%ld %ld %ld\n",Tempstep, Nleft, Nrigth);
               fgets(string_temp,80,file1);
      }
  printf("%ld\r", i++);
 }/** end while **/

   printf("\n");
  /** fclose closes files **/
  fclose(file1);
  printf("file1 is closed\n");
  fclose(file2);
  printf("file2 is closed\n");
  fclose(file3) ;    /** NOTE 2 **/
  printf("file3 is closed\n");
  return 0;
} /** end main */
/************************************/
/********Input file*****/
ITEM: TIMESTEP
    6770000
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6771000
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6772000
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6773000
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
ITEM: TIMESTEP
    6774000
ITEM: ATOMS
 1  1  0.44832  0.49731  0.50099    0    0    0
 2  1  0.44982  0.49486  0.49859    0    0    0
 3  1  0.45148  0.49378  0.49618    0    0    0
 4  1  0.45286  0.49414  0.49380    0    0    0
 5  1  0.45444  0.49446  0.49117    0    0    0
/************************************************/

Answer
Again you missed to explain about the program:)

With regard to "segmentation fault", it can happen for many reasons and it your file pointer is already closed or NULL, it can cause segmentation fault.
So, it is better to get the core file and use gdb to see where exactly it has crashed.
If you don't know, 'gdb' is GNU debugger/.
On a Solaris or Linux system, compile your code with -g flag.
Execute the program and let it crash and it must have dumped core.
Now execute:
gdb <executable> core
gdb> where
This will show the exact place where the crash has happened.
After that, it will be easy for you to analyze.

I copied your code to my system and gave the input file that you have given above.
It didn't seg fault.

-Narendra

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.