Linked list

  • Thread starter Alexander Hunziker
  • Start date
A

Alexander Hunziker

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 =====

#include "stl.h"

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);
}
 
N

Netocrat

I'm trying to program a linked list, but unfortunately it segfaults when I
run the program. [snip]
The program segfaults in the while loop in list_append during the third
call of list_append. [snip code]
cur_triangle = (nodeptr) malloc(sizeof(nodeptr));

Try instead:

cur_triangle = malloc(sizeof(*nodeptr));
 
A

Alexander Hunziker

cur_triangle = malloc(sizeof(*nodeptr));

The compiler won't accept that. nodeptr ist typdedeffed as being a
pointer to a struct node. Other ideas?

Alex
 
F

forayer

Alexander said:
The compiler won't accept that. nodeptr ist typdedeffed as being a
pointer to a struct node. Other ideas?

Alex
This should be obvious:
cur_triangle = malloc( sizeof(struct node) );

You need to allocate enough memory to hold the struct, not a pointer to
the struct!


cheers,
forayer
 
K

Keith Thompson

Netocrat said:
I'm trying to program a linked list, but unfortunately it segfaults when I
run the program. [snip]
The program segfaults in the while loop in list_append during the third
call of list_append. [snip code]
cur_triangle = (nodeptr) malloc(sizeof(nodeptr));

Try instead:

cur_triangle = malloc(sizeof(*nodeptr));

No, that won't work, since nodeptr is a typedef for struct node *.

The idiom is:

cur_triangle = malloc(sizeof *cur_triangle);

Using the typedef nodeptr is probably not a good idea, since it hides
the fact that it's a pointer type (though the "ptr" suffix helps, of
course). I suggest using "struct node *" instead.
 
M

Martin Ambuhl

Alexander said:
The compiler won't accept that. nodeptr ist typdedeffed as being a
pointer to a struct node. Other ideas?

if
cur_triangle = malloc(sizeof *cur_triangle);
doesn't work, then you are trying to do something very suspect.

Since your total preserved context is quoted above, it's hard to say
*what* you might be doing wrong.
 
N

Netocrat

The compiler won't accept that. nodeptr ist typdedeffed as being a pointer
to a struct node. Other ideas?

As others have pointed out my suggestion was wrong - I mistook nodeptr for
the variable when it is actually the type... but the idea is that you need
to allocate memory for what is pointed to, not what points to it. So just
change what I wrote to (using the variable):

cur_triangle = malloc(sizeof(*cur_triangle));

or the less maintainable (using the type):

cur_triangle = malloc(sizeof(struct node));
 
C

CBFalconer

Martin said:
if
cur_triangle = malloc(sizeof *cur_triangle);
doesn't work, then you are trying to do something very suspect.

Since your total preserved context is quoted above, it's hard to
say *what* you might be doing wrong.

There are several faults involved, (besides the code). One person
(Hunziker) snipped attributions of quoted material, and another
(unknown and possibly Hunziker) snipped so much that the article no
longer makes sense.

Guideline: First snip the material that is not germane, and leave
what is germane. The results, with your answer, should be fairly
complete and understandable by itself. Then remove attribution
lines only when all the attributable material is gone.

Whatever you post on Usenet is going up on a monstrous world-wide
visible bulletin board, and you have no control over what else is
up there, or has been ripped down.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top