Linked list

Discussion in 'C Programming' started by Alexander Hunziker, Jul 3, 2005.

  1. Hello everybody,

    I'm trying to program a linked list, but unfortunately it segfaults when
    I run the program. The code is basically the same as one can find in
    several places in the internet. With gdb I found out, that my root->next
    points to some strange memory area instead of where it should.

    The program segfaults in the while loop in list_append during the third
    call of list_append.

    Can somebody help me? I'll also gladly send the whole code and Makefile
    to somebody personally if he/she's willing to look at it.

    Thanks,
    Alex

    Here's my code:

    ===== stl.h =====

    typedef struct {
    float x, y, z;
    } point;

    typedef struct {
    point n;
    point v1;
    point v2;
    point v3;
    } triangle;

    typedef struct node *nodeptr;

    typedef struct node{
    triangle tri;
    nodeptr next; //Zeiger auf einen Listenknoten
    } node;

    void list_append(nodeptr node);




    ===== list.c =====

    #include "stl.h"

    nodeptr root = NULL;

    void list_append(nodeptr node)
    {
    nodeptr cur_node;

    if(root == NULL){
    root = node;
    root->next= NULL;
    } else {
    cur_node = root;
    while (cur_node->next != NULL)
    cur_node = cur_node->next;
    cur_node->next = node;
    cur_node = cur_node->next;
    cur_node->next = NULL;
    }
    }



    ===== main.c =====

    nodeptr cur_triangle

    for(i = 1; i < number_of_triangles; i++)
    {
    // Values are being read into temptriangle array
    cur_triangle = (nodeptr) malloc(sizeof(nodeptr));
    cur_triangle->tri.n.x = temptriangle[0];
    cur_triangle->tri.n.y = temptriangle[1];
    cur_triangle->tri.n.z = temptriangle[2];
    cur_triangle->tri.v1.x = temptriangle[3];
    cur_triangle->tri.v1.y = temptriangle[4];
    cur_triangle->tri.v1.z = temptriangle[5];
    cur_triangle->tri.v2.x = temptriangle[6];
    cur_triangle->tri.v2.y = temptriangle[7];
    cur_triangle->tri.v2.z = temptriangle[8];
    cur_triangle->tri.v3.x = temptriangle[9];
    cur_triangle->tri.v3.y = temptriangle[10];
    cur_triangle->tri.v3.z = temptriangle[11];

    list_append(cur_triangle);
    }
     
    Alexander Hunziker, Jul 3, 2005
    #1
    1. Advertising

  2. Alexander Hunziker

    BGreene Guest

    Has nothing to do with C
    "Alexander Hunziker" <> wrote in message
    news:...
    > Hello everybody,
    >
    > I'm trying to program a linked list, but unfortunately it segfaults when
    > I run the program. The code is basically the same as one can find in
    > several places in the internet. With gdb I found out, that my root->next
    > points to some strange memory area instead of where it should.
    >
    > The program segfaults in the while loop in list_append during the third
    > call of list_append.
    >
    > Can somebody help me? I'll also gladly send the whole code and Makefile
    > to somebody personally if he/she's willing to look at it.
    >
    > Thanks,
    > Alex
    >
    > Here's my code:
    >
    > ===== stl.h =====
    >
    > typedef struct {
    > float x, y, z;
    > } point;
    >
    > typedef struct {
    > point n;
    > point v1;
    > point v2;
    > point v3;
    > } triangle;
    >
    > typedef struct node *nodeptr;
    >
    > typedef struct node{
    > triangle tri;
    > nodeptr next; //Zeiger auf einen Listenknoten
    > } node;
    >
    > void list_append(nodeptr node);
    >
    >
    >
    >
    > ===== list.c =====
    >
    > #include "stl.h"
    >
    > nodeptr root = NULL;
    >
    > void list_append(nodeptr node)
    > {
    > nodeptr cur_node;
    >
    > if(root == NULL){
    > root = node;
    > root->next= NULL;
    > } else {
    > cur_node = root;
    > while (cur_node->next != NULL)
    > cur_node = cur_node->next;
    > cur_node->next = node;
    > cur_node = cur_node->next;
    > cur_node->next = NULL;
    > }
    > }
    >
    >
    >
    > ===== main.c =====
    >
    > nodeptr cur_triangle
    >
    > for(i = 1; i < number_of_triangles; i++)
    > {
    > // Values are being read into temptriangle array
    > cur_triangle = (nodeptr) malloc(sizeof(nodeptr));
    > cur_triangle->tri.n.x = temptriangle[0];
    > cur_triangle->tri.n.y = temptriangle[1];
    > cur_triangle->tri.n.z = temptriangle[2];
    > cur_triangle->tri.v1.x = temptriangle[3];
    > cur_triangle->tri.v1.y = temptriangle[4];
    > cur_triangle->tri.v1.z = temptriangle[5];
    > cur_triangle->tri.v2.x = temptriangle[6];
    > cur_triangle->tri.v2.y = temptriangle[7];
    > cur_triangle->tri.v2.z = temptriangle[8];
    > cur_triangle->tri.v3.x = temptriangle[9];
    > cur_triangle->tri.v3.y = temptriangle[10];
    > cur_triangle->tri.v3.z = temptriangle[11];
    >
    > list_append(cur_triangle);
    > }
    >
     
    BGreene, Jul 6, 2005
    #2
    1. Advertising

  3. "BGreene" <> writes:
    > Has nothing to do with C
    > "Alexander Hunziker" <> wrote in message
    > news:...
    >> Hello everybody,
    >>
    >> I'm trying to program a linked list, but unfortunately it segfaults when
    >> I run the program. The code is basically the same as one can find in
    >> several places in the internet. With gdb I found out, that my root->next
    >> points to some strange memory area instead of where it should.

    [...]

    First, please don't top-post.

    Second, what makes you think the question has nothing to do with C?

    Third, we answered the OP's question several days ago (the problem was
    an incorrect argument to malloc()).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jul 6, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Ritchey
    Replies:
    7
    Views:
    484
    emerth
    Jul 10, 2003
  2. Chris Ritchey

    Generating a char* from a linked list of linked lists

    Chris Ritchey, Jul 9, 2003, in forum: C Programming
    Replies:
    7
    Views:
    472
    emerth
    Jul 10, 2003
  3. fool
    Replies:
    14
    Views:
    512
    Barry Schwarz
    Jul 3, 2006
  4. joshd
    Replies:
    12
    Views:
    674
    John Carson
    Oct 2, 2006
  5. jawdoc
    Replies:
    9
    Views:
    760
    Chris Thomasson
    Mar 10, 2008
Loading...

Share This Page