Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Linked list

Reply
Thread Tools

Linked list

 
 
Alexander Hunziker
Guest
Posts: n/a
 
      07-03-2005
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);
}

 
Reply With Quote
 
 
 
 
BGreene
Guest
Posts: n/a
 
      07-06-2005
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);
> }
>



 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-06-2005
"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) kst- <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Airplane Program with Linked Lists. The linked list portion is veryconfusing to me. jawdoc C++ 9 03-10-2008 03:38 AM
Linked list within a linked list joshd C++ 12 10-02-2006 08:57 AM
Linked list, New try (was:Linked list, no out put,help) fool C Programming 14 07-03-2006 12:29 AM
Generating a char* from a linked list of linked lists Chris Ritchey C++ 7 07-10-2003 10:12 PM
Generating a char* from a linked list of linked lists Chris Ritchey C Programming 7 07-10-2003 10:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57