Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    87

    Talking Segmentation fault

    I'm getting a frustrating segmentation fault, someone help me understand why.

    Code:
    /*Print the process information*/
    void printDetails(char* filename,char* path){
      FILE *f_name;
      int pid,tty,stime;
      char pname[FILENAME_MAX];
      char* t_name,time;
    
      /*Format the path*/
      strcat(path,"/");
      strcat(path,"stat");
    
      if((f_name = fopen(path,"r")) != NULL){
        fscanf(f_name,"%d %s %*c %*d %*d %*d %d %*d 
               %*u %*u %*u %*u %*u %*d %d",&pid,&pname,&tty,&stime);
        t_name = ttyname(tty);
        printf("B4 getTime\n");
        time = getTime(stime); /*HERE IT IS - THE BASTARD*/
        if(t_name == NULL)
          t_name = "?";           
        printf("%-9d%-12s%-6s  %s\n",pid,t_name,time,pname);
        fclose(f_name);
      }else
        fprintf(stderr,"Can't open %s\n",path);
    
    }
    
    char* getTime(int clockticks){
      float jiffies,result;
      int min,hr,seconds;
      char* time;
    
      jiffies = 0.01f;
      
      result  = jiffies * clockticks;
      seconds = result;
      min     = seconds/60;
      hr      = min/60;
    
      seconds %= 60; /*MOD it into 0-59*/
      min     %= 60; /*MOD it into 0-59*/
    
      if(hr < 10 && min < 10 && seconds < 10)
        sprintf(time,"0%d:0%d:0%d",hr,min,seconds);
      else if(hr < 10 && min < 10)
        sprintf(time,"0%d:0%d:%d",hr,min,seconds);
      else if(hr < 10 && seconds < 10)
        sprintf(time,"0%d:%d:0%d",hr,min,seconds);
      else if(min < 10 && seconds < 10)
        sprintf(time,"%d:0%d:0%d",hr,min,seconds);
      else if(hr < 10)
        sprintf(time,"0%d:%d:%d",hr,min,seconds);
      else if(min < 10)
        sprintf(time,"%d:0%d:%d",min,hr,seconds);
      else if(seconds < 10)
        sprintf(time,"%d:%d:0%d",min,hr,seconds);
      else
        sprintf(time,"%d:%d:%d",min,hr,seconds);
      
      return time;
    }
    PuterPaul.co.uk - Portfolio site

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The bastard is variable time. It is of type char and function getTime returns a pointer to char.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >char* t_name,time;
    should be
    char *t_name, *time;

    It's cases such as these that you should bind the * with the variable and not the type so that you can see more easily the problem. It may seem nitpicky (it's just a question of a space), but if the * is connected to the type you tend to think that it means all variables will be pointers, when it doesn't work that way.

    >sprintf(time,"0%d:0%d:0%d",hr,min,seconds);
    This will cause a problem too, time is a pointer and you're treating it like an array. Pointers and arrays are NOT the same, you need to allocate some memory to time or the program will choke.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    87
    Changed it to this

    Code:
    char* t_name;
    char* time;
    So time is a char* , same happens tho, am I being dumb?? I have a nasty feeling I am.
    PuterPaul.co.uk - Portfolio site

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >same happens tho
    If you didn't allocate memory for the time variable in the getTime function then I'm not surprised. You're assigning data to memory that you do not own. This is illegal and will result in a segmentation violation.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM