Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <4E128F5A.157A@mindspring.com> (permalink) |
|---|---|
| Date | 2011-07-05 00:13 -0400 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| Newsgroups | comp.lang.c |
| Subject | Re: Linked-list problem - compiles but segfaults |
| References | <0ae24ce7-2bd8-4493-989b-0df3bd0ff263@g12g2000yqd.googlegroups.com> |
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 <ctype.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/stat.h>
>
> /* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Linked-list problem - compiles but segfaults Andy Elvey <neptune.poseidon789@gmail.com> - 2011-07-04 17:53 -0700
Re: Linked-list problem - compiles but segfaults "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-07-04 22:18 -0400
Re: Linked-list problem - compiles but segfaults Andy Elvey <neptune.poseidon789@gmail.com> - 2011-07-04 19:31 -0700
Re: Linked-list problem - compiles but segfaults gordonb.bxelr@burditt.org (Gordon Burditt) - 2011-07-04 21:39 -0500
Re: Linked-list problem - compiles but segfaults Andy Elvey <neptune.poseidon789@gmail.com> - 2011-07-04 20:00 -0700
Re: Linked-list problem - compiles but segfaults gordonb.tu82f@burditt.org (Gordon Burditt) - 2011-07-05 00:11 -0500
Re: Linked-list problem - compiles but segfaults pete <pfiland@mindspring.com> - 2011-07-05 00:13 -0400
Re: Linked-list problem - compiles but segfaults Andy Elvey <neptune.poseidon789@gmail.com> - 2011-07-04 21:20 -0700
Re: Linked-list problem - compiles but segfaults pete <pfiland@mindspring.com> - 2011-07-05 01:04 -0400
Re: Linked-list problem - compiles but segfaults "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2011-07-05 08:42 -0700
Re: Linked-list problem - compiles but segfaults Keith Thompson <kst-u@mib.org> - 2011-07-05 09:04 -0700
Re: Linked-list problem - compiles but segfaults Paul N <gw7rib@aol.com> - 2011-07-05 14:51 -0700
Re: Linked-list problem - compiles but segfaults Todd Carnes <toddcarnes@gmail.com> - 2011-07-06 03:20 +0000
Re: Linked-list problem - compiles but segfaults Shao Miller <sha0.miller@gmail.com> - 2011-07-05 23:37 -0400
Re: Linked-list problem - compiles but segfaults Shao Miller <sha0.miller@gmail.com> - 2011-07-05 23:41 -0400
Re: Linked-list problem - compiles but segfaults Shao Miller <sha0.miller@gmail.com> - 2011-07-05 23:44 -0400
Re: Linked-list problem - compiles but segfaults Shao Miller <sha0.miller@gmail.com> - 2011-07-05 23:54 -0400
Re: Linked-list problem - compiles but segfaults "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-07-06 07:51 -0400
Re: Linked-list problem - compiles but segfaults Shao Miller <sha0.miller@gmail.com> - 2011-07-06 09:50 -0400
csiph-web