Thread: segmentation fault error [help]

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    segmentation fault error [help]

    Hey, I'm still in the process of learning C and need some help. So I'm working with data structures and having trouble making a tree of strings where every node has a passengers name, number of the city they are at and number of the the city they want to go to. the tree is aligned by the passengers names.
    My problems are how to deal with the name string and inserting the passenger that i read into the tree.

    This is what I came up with till now.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 50
    
    struct passengerNode{   
        char *name;
        int currentCity;
        int destination;
        struct passengerNode *leftPtr;
        struct passengerNode *rightPtr;
    };
    typedef struct passengerNode PassengerNode;
    typedef PassengerNode *PassengerNodePtr;
    
    PassengerNodePtr readPassenger(void);
    void insertPassenger(PassengerNodePtr *passengerPtr,char *name);
    
    
    int main(int argc, char*argv[]){
        PassengerNodePtr newPass;
        newPass = readPassenger();
        insertPassenger(&newPass,newPass->name);
        return 0;
    }
    
    PassengerNodePtr readPassenger(void){
        
        PassengerNodePtr pass = malloc(sizeof(struct passengerNode));
    
        printf("Name: ");
        fgets(pass->name,MAX,stdin);
    
        printf("Source city: ");
        scanf("%d",&(pass->currentCity));
    
        printf("destination city: ");
        scanf("%d",&(pass->destination));
    
        return pass;
    }       
    
    void insertPassenger(PassengerNodePtr *passengerPtr, char *name){
        if(*passengerPtr == NULL){
            *passengerPtr = malloc(sizeof(struct passengerNode));
            if (*passengerPtr !=NULL){
                (*passengerPtr )->leftPtr = NULL;
                (*passengerPtr )->rightPtr = NULL;
            }else{
                if(strcmp(name,(*passengerPtr)->name)== -1){
                    insertPassenger(&(*passengerPtr)->leftPtr, name);
                }else if(strcmp(name,(*passengerPtr)->name) == 1 || strcmp(name,(*passengerPtr)->name) == 0){
                    insertPassenger(&(*passengerPtr)->rightPtr,name);
                }
            }
        }
    }
    Last edited by blaze505; 10-21-2011 at 02:58 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    If you get a segfault, your first step should always be to run the program through a debugger.

    If it's available for your platform, I recommend Valgrind. It can often tell you where the problem is instead of where the fault occurred.

    Otherwise, try something like gdb, or whatever your platform's native debugger is.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Doesn't even compile!, how did you manage to get a runtime error?!

    EDIT: Ok, you corrected it...
    Devoted my life to programming...

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The segfault occurs inside "fgets()". Check your code around there. My guess is a wrong memory allocation.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    PassengerNodePtr pass = malloc(sizeof(struct passengerNode));
     
    printf("Name: ");
    fgets(pass->name,MAX,stdin);
    You allocate space for a passengerNode object. That has a member called name which is a char pointer. That pointer is uninitialized, and doesn't point to memory you own, so you can't read into it. You need to malloc some space for pass->name before reading into it, or make it a char array in the structure declaration.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    Yes thank you, I just made it a char array in the structure and now it works.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > strcmp(name,(*passengerPtr)->name)== -1
    According to the standard, you should only test for < 0 , == 0 and > 0
    strcmp() is allowed to return any negative value if s1 is before s2 (not specifically -1)
    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.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by blaze505 View Post
    Hey, I'm still in the process of learning C and need some help. So I'm working with data structures and having trouble making a tree of strings where every node has a passengers name, number of the city they are at and number of the the city they want to go to. the tree is aligned by the passengers names.
    My problems are how to deal with the name string and inserting the passenger that i read into the tree.

    This is what I came up with till now.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 50
    
    struct passengerNode{   
        char *name;
        int currentCity;
        int destination;
        struct passengerNode *leftPtr;
        struct passengerNode *rightPtr;
    };
    typedef struct passengerNode PassengerNode;
    typedef PassengerNode *PassengerNodePtr;
    
    PassengerNodePtr readPassenger(void);
    void insertPassenger(PassengerNodePtr *passengerPtr,char *name);
    
    
    int main(int argc, char*argv[]){
        PassengerNodePtr newPass;
        newPass = readPassenger();
        insertPassenger(&newPass,newPass->name);
        return 0;
    }
    
    PassengerNodePtr readPassenger(void){
        
        PassengerNodePtr pass = malloc(sizeof(struct passengerNode));
    
        printf("Name: ");
        fgets(pass->name,MAX,stdin);
    
        printf("Source city: ");
        scanf("%d",&(pass->currentCity));
    
        printf("destination city: ");
        scanf("%d",&(pass->destination));
    
        return pass;
    }       
    
    void insertPassenger(PassengerNodePtr *passengerPtr, char *name){
        if(*passengerPtr == NULL){
            *passengerPtr = malloc(sizeof(struct passengerNode));
            if (*passengerPtr !=NULL){
                (*passengerPtr )->leftPtr = NULL;
                (*passengerPtr )->rightPtr = NULL;
            }else{
                if(strcmp(name,(*passengerPtr)->name)== -1){
                    insertPassenger(&(*passengerPtr)->leftPtr, name);
                }else if(strcmp(name,(*passengerPtr)->name) == 1 || strcmp(name,(*passengerPtr)->name) == 0){
                    insertPassenger(&(*passengerPtr)->rightPtr,name);
                }
            }
        }
    }

    At line 30, you allocate memory for the struct but you do not allocate memory for the passenger's name.

    This leaves you two choices...

    A call malloc() again to allocate memory for the name... keeping in mind that you will have to free it later.
    OR
    Modify your struct with a fixed size buffer built in...
    Code:
    struct passengerNode{   
        char name[MAX];
        int currentCity;
        int destination;
        struct passengerNode *leftPtr;
        struct passengerNode *rightPtr;
    };
    Personally I would favor the second approach as it's far less likely to leak memory.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Also, your recursive calls to insertPassenger() can basically never happen (unless you run out of memory, then things get really horrible!).
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Also, your recursive calls to insertPassenger() can basically never happen (unless you run out of memory, then things get really horrible!).
    I'd like to see the train that could handle a 16gb passenger list....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is Segmentation fault and why do we get this error?
    By sumit180288 in forum C Programming
    Replies: 2
    Last Post: 09-12-2011, 12:05 AM
  2. Segmentation Fault Error
    By ruanhupo in forum C Programming
    Replies: 2
    Last Post: 04-19-2011, 01:57 AM
  3. Segmentation Fault Error
    By c3jcarmy in forum C Programming
    Replies: 4
    Last Post: 01-25-2011, 09:48 AM
  4. Segmentation fault Error
    By unknown_ in forum C Programming
    Replies: 7
    Last Post: 03-21-2010, 01:32 PM
  5. Segmentation Fault Error
    By Taka in forum C Programming
    Replies: 5
    Last Post: 09-15-2007, 12:55 PM