Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #7647

Re: Linked-list problem - compiles but segfaults

Message-ID <4E129B5E.6318@mindspring.com> (permalink)
Date 2011-07-05 01:04 -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> <4E128F5A.157A@mindspring.com> <b1b0e240-c20c-46eb-bb24-f9d180bce219@t9g2000vbv.googlegroups.com>

Show all headers | View raw


Andy Elvey wrote:
> 
> On Jul 5, 4:13 pm, pete <pfil...@mindspring.com> wrote:
> > 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
> 
> Many thanks for that, Pete!  That code is the best that I've seen for
> a linked list!
> It's good to see some code that's easy to understand.
> Thanks again - bye for now -

You're welcome.
You can also use the same list_append() and list_free()
functions for other data types as well.

/* 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_int_fprintf(List *node, FILE *stream)
{
    int rc = 0;
    
    while (node != NULL
        && (rc = fprintf(stream, "%d\n", *(int *)node->data)) > 0) 
    {
        node = node -> next; 
    }
    return rc;
}

/* End linked list implementation */

int 
main(void)
{
    /* Declare a list */
    List *mylist = NULL;
    List *tail = NULL;
    int i[] = {1, 2, 42};
    int *p = i;

    do {
        tail = list_append(&mylist, tail, p, sizeof *p);
    } while(tail != NULL && ++p != i + sizeof i / sizeof *i);
    puts("/* BEGIN new.c output */\n");
    list_int_fprintf(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


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