Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

seg fault - link list within a link list

Status
Not open for further replies.

ezeke1

Programmer
Mar 20, 2003
41
US
The idea is to have a singly link list of nodes and each node has a local table of data represented by another link list. I know where the seg fault is coming from, but I don't know why. Rather than paste my whole program, I typed up this stub that shows what Im trying to do. If there are typos, please ignore as the program compiles. Can anyone shed some insight into what I may be doing wrong?

Code:
typedef struct table {
   char *name;
   struct table *next;
} table_rec, *table_ptr;

typedef struct list {
   char *listname;
   struct list *next;
   table_ptr   table;
} list_rec, *list_ptr;

list_ptr new_list(char *name);
table_ptr new_table(char *name);
list_ptr LIST;

void main() {
   list_ptr a = new_list("first");
   a->next = LIST;
   LIST = a;    //list is a->null

   list_ptr b = new_list("second");
   b->next = LIST;
   LIST = b;    //list is b->a

   //now create local data for second node
   table_ptr ta = new_table("foo"); //this is fine
   ta->next = LIST->table; //this causes seg fault
   LIST->table = ta; //this causes seg fault
}

list_ptr new_list(char *name) {
    //create node
    list_ptr temp = (list_ptr)malloc(sizeof(list_rec));
    temp->name = (char*)strdup(name);
    temp->next = NULL;
    temp->table = NULL;
    return temp;
}

table_ptr new_table(char *name) {
    //create node
    table_ptr temp = (table_ptr)malloc(sizeof(table_rec));
    temp->name = (char*)strdup(name);
    temp->next = NULL;
    return temp;
}
 
I tested the code some more and the problem appears to be caused by something else, please disregard this post.

 
You should never ignore typos or any other kind of errors.
You only need one of those typos to actually cause a problem and you are ignoring a ticking timebomb.
I tried to compile your code and got a bunch of errors.
I was gonna fix them all and check the code out but since you say the problem is elsewhere I wont bother.



Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top