Thread: Help Getting Segmentation fault

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Help Getting Segmentation fault

    Hi im new to the C language and I dont understant why im getting a Segmentation fault. Please help.

    twi.c

    insert
    Code:
    #include "twi.h"
    
    /*char* readInput
     *return the entire line
     */
    char* getLine()
    {
      char *temp;
      temp = (char*)malloc(sizeof(char[176]));
      fgets(temp,176,stdin);
      return temp;
    }
    
    /*void print
     *accpet a struct twi
     *Display info in twi
     */
    void print(tData input)
    {
      printf("@%s %s %s %s\n", input->name, input->date, input->time, input->msg);
    }
    
    /*char* findMsg
     *accept char*
     *return char* the msg in the string which is 3 isspace away.
     */
    char* findMsg(char *input)
    {
      char *p;
      p = input;
      int count = 0;
      while(isspace(*p) || count != 3)
        {
          if(isspace (*p))
        count++;    
          ++p;
        };
       char *p2 = input;
      int run;
      run = (p-input);
      while(run!=0)
        {
          *p2++;
          run--;
        }
      return p2;
    }
    
    /*int make
     *create a struct using char* provide
     *return int for checking
     */
    int make(tData temp, char* input)
    {
      int check;
      //create temporary variable
      char nameSize[15];
      char timeSize[10];
      char dateSize[10];
      check =  sscanf(input, "%s %s %s", nameSize,dateSize,timeSize);
      //allocate memory depending on length of char
      temp->name = (char*)malloc(sizeof(strlen(nameSize)));
      temp->date = (char*)malloc(sizeof(strlen(dateSize)));
      temp->time = (char*)malloc(sizeof(strlen(timeSize)));
      temp->msg = (char*)malloc(sizeof(strlen(findMsg(input))));
      //copying into struct
      strcpy(temp->name, nameSize);
      strcpy(temp->date, dateSize);
      strcpy(temp->time, timeSize);
      strcpy(temp->msg, findMsg(input)); 
      // printf("%s %s %s %s",temp->name,temp->date,temp->time, temp->msg);
      if(check == 3)
        return check;
      else
        return 0;
    }

    p3.c

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "twi.h"
    
    main()
    {
    
      tData t[100];
      int i = 0;
      char* str;
      int check = 3;
      t[0] = (struct twi*)malloc(sizeof(char[300])); 
      str = getLine();
      while(make(t[i],str) == 3)
        {  
          str = getLine();
          i++;
          // t[i] = (struct twi*)malloc(sizeof(struct twi)); 
        
        }
      int x;
      for(x = 0; x < i-1; x++)
        print(t[x]);
      
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > temp->name = (char*)malloc(sizeof(strlen(nameSize)));
    Doing this will always get you sizeof(size_t), which is what strlen() returns.

    You need
    temp->name = (char*)malloc(strlen(nameSize)+1);

    One more thing, read the FAQ on casting the return result of malloc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    My guess is the your print function:
    Code:
    void print(tData input)
    {
        printf("@%s %s %s %s\n", input->name, input->date, input->time, input->msg);
    }
    You pass in a tData structure, but use the "pointer to struct" operator ->. Try passing in a tData pointer instead of the whole struct (saves space on the stack).
    Code:
    void print (tData *input)
    ...
    print(&t[x]);
    It's hard to say for sure though, without seeing how you typedef'ed tData (presumably in twi.h). There are several other suggestions I will make too:
    1. It's int main(void) and you will need a return at the end of main too: Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
    2. Don't cast malloc: Cprogramming.com FAQ > Casting malloc
    3. You malloc like crazy, but there isn't a single free anywhere in your program. You should have a corresponding free for every malloc (i.e. free str after each getLine call).
    4. In make(): sizeof(strlen(nameSize)) should just be strlen(nameSize) + 1 (enough room for the name plus a null character).

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Found my problem

    Found out has to do with one of my variable not reading the the right data.

    I gave a txt file with the following on it

    name 2010-11-29 21:30:00 msg that has a max of 140 char

    When I do a scanf("%s %s %s", temp->name, temp->date, temp->time);

    i get temp->date as 2010-11-29 21:30:00
    and
    temp->time as 21:30:00

    why is this happening should it stop once it hit the space?

    thank for helping

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You should not be copying all these strings...
    Code:
    strcpy(temp->name, nameSize);
    strcpy(temp->date, dateSize);
    strcpy(temp->time, timeSize);
    if your sscanf didn't return 3. It's a waste of mallocs, and who knows if the strings nameSize, dateSize, timeSize are even null terminated.

    Instead, do the test first and exit out earlier.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    sscanf did read everything correctly, it's just that you only gave 10 characters for dateSize. You have a 10 character date, but it needed to put a null in the 11th spot, which would be the first character of timeSize. Then it read in timeSize, null-terminated that string and covered up the null from dateSize. dateSize doesn't know that it's supposed to stop after 10 characters, it just goes until it finds the null at the end of timeSize (they're sequential on the stack), so it looks like one giant string and like timeSize is indexing the middle of that string where the time starts. You always need +1 character for the null, so make dateSize bigger.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    We haven't seen the struct tData yet. So perhaps if its fields aren't big enough you'll get overflows there too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2010, 10:55 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

Tags for this Thread