Thread: Segmentation fault on linked list

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    16

    Segmentation fault on linked list

    I created a hashtable with separate chaining where I put structs called Chores, here is my implementation and struct:

    Code:
    typedef struct Chore{
        char name [8000];
        unsigned long id, duration, *deps;
        int hasDeps;
    }*pChore;
    
    typedef struct nodehash{       /*Node of list*/
        pChore obj;
        struct nodehash*next;
    }*link;
    
    And here is my functions to search a Chore by its id:

    Code:
    pChore search(unsigned long id){
        int i = hash(id, M);
        return searchAux(heads[i], id);
    }
    
    pChore searchAux(link h, unsigned long id){
        link x, t = h;
        for(t = h;t != NULL; t = t->next){
            if(t->obj != NULL && t->obj->id == id)
                x = t;
    
        }
        return x->obj;
    }
    
    I keep getting seg fault on the searchAux function and I don't know why, I feel I'm transversing the list pretty well, can someone figure out what's wrong?

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    1. set x to an initial value of NULL
    2. return NULL if x is NULL
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault in linked list program
    By smrtshrm in forum C Programming
    Replies: 5
    Last Post: 06-26-2015, 06:00 AM
  2. Segmentation Fault -- Stack implementing a Linked List
    By jackalclone1 in forum C Programming
    Replies: 10
    Last Post: 02-15-2014, 12:44 AM
  3. Segmentation fault in linked list...doubt
    By alphasil in forum C Programming
    Replies: 17
    Last Post: 07-11-2012, 05:23 PM
  4. Doubly linked list-segmentation fault
    By prapanch in forum C Programming
    Replies: 2
    Last Post: 05-10-2012, 11:04 PM
  5. Doubly Linked List Segmentation Fault
    By DaNxTh3xMaNx in forum C Programming
    Replies: 5
    Last Post: 09-09-2011, 09:50 AM

Tags for this Thread