Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 04 Jul 2011 23:13:14 -0500 Message-ID: <4E128F5A.157A@mindspring.com> Date: Tue, 05 Jul 2011 00:13:14 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Linked-list problem - compiles but segfaults References: <0ae24ce7-2bd8-4493-989b-0df3bd0ff263@g12g2000yqd.googlegroups.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 171 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.231.76 X-Trace: sv3-CCR1sQOzqWP9+Njs4cNL6oqGaLsDDH3Y2UrF9kA+zQWdoyB00vMsuv/AB1KVLMPoteVCIWwSo/Bcf9a!nOzIqry70L9v/1SPyFxFSLZM0z8r3QS+5VVw/ETAkGTpBpRrup2lUPEw7eZrNRruJQZfA/nHpfmL!MyjRjnL7CA== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4923 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:7639 Andy Elvey wrote: > > Hi all - > I've been poking around with C for a while, but I have big problems > with linked lists. This seems to be mainly around the "declaration, > initialisation, iterating" area. Anyway, I have the code below, which > compiles but gives a segfault (and I have no idea why), so hoping that > someone may be able to help. > ( I have three puts statements at the end because I'm unsure how to > iterate along a list. ) > [code] > /* This code is released to the public domain */ > #include > #include > #include > #include > #include > > /* Linked list implementation */ > typedef struct _List List; > > struct _List { > void *data; > List *next; > }; > > static List *list_malloc (void) > { > return (List *) malloc (sizeof (List)); > } > > List *list_prepend (List *list, void *data) > { > List *ret = list_malloc (); > ret->data = data; > ret->next = list; > return ret; > } > > List *list_append (List *list, void *data) > { > List *ret; > if (list == NULL) return list_prepend (list, data); > ret = list; > for (; list->next; list = list->next); > list->next = list_malloc (); > list->next->data = data; > list->next->next = NULL; > return ret; > } > > void list_free (List *list) > { > List *next; > while (list) > { > next = list->next; > free (list); > list = next; > } > } > > /* End linked list implementation */ > > int main() > { > /* Declare a list */ > List *mylist = NULL; > list_append(mylist, "foo"); > puts(mylist->data); > list_append(mylist, (int*) 42); > puts(mylist->data); > list_append(mylist, "baz"); > puts(mylist->data); > return 0; > } > [/code] > Very many thanks if you can get this code working...... :) > - Andy /* BEGIN new.c */ #include #include #include /* Linked list implementation */ typedef struct LIST { struct LIST *next; void *data; } List; List * list_append(List **head, List *tail, const void *data, size_t size) { List *node; node = malloc(sizeof *node); if (node != NULL) { node -> next = NULL; node -> data = malloc(size); if (node -> data != NULL) { memcpy(node -> data, data, size); if (*head != NULL) { tail -> next = node; } else { *head = node; } } else { free(node); node = NULL; } } return node; } void list_free(List *node, void (*free_data)(void *)) { List *next_node; while (node != NULL) { next_node = node -> next; free_data(node -> data); free(node); node = next_node; } } int list_fputs(List *node, FILE *stream) { int rc = 0; while (node != NULL && (rc = fputs(node -> data, stream)) != EOF && (rc = putc( '\n', stream)) != EOF) { node = node -> next; } return rc; } /* End linked list implementation */ int main(void) { /* Declare a list */ List *mylist = NULL; List *tail = NULL; char *w[] = {"foo", "bar", "bazooka"}; char **p = w; do { tail = list_append(&mylist, tail, *p, 1 + strlen(*p)); } while(tail != NULL && ++p != w + sizeof w / sizeof *w); puts("/* BEGIN new.c output */\n"); list_fputs(mylist, stdout); list_free(mylist, free); puts("\n/* END new.c output */"); return 0; } /* END new.c */ -- pete