1

I can't figure out why my code causes a segfault when I try the following give these structs:

typedef struct Vertex {
    int i;
    int color;
} vertex;

typedef struct Edge {
    vertex v1;
    vertex v2;
} edge;

typedef struct Node {
    vertex *v;
    struct Node *next;
} node;

node *nodehead = NULL;

code causing the issue here:

nodehead = malloc(sizeof(node));
if (nodehead == NULL) return -1;
nodehead->v->i = 10;
nodehead->next = NULL;

Maybe I'm missing something, most people had issues because they were trying to use the -> operator on an object. It seems trying to assign the int to the nodehead->v->i field causes the issue.

5
  • 2
    assign something to nodehead->v before using it Commented Jan 10, 2019 at 19:39
  • 1
    The malloc allocates space for the two pointers inside struct Node; you do not have any memory allocated for those pointers to point to. That is, there is no struct Vertex behind nodehead->v Commented Jan 10, 2019 at 19:40
  • @Hogan Wrong. He's not dereferncing i. Look again. The problem is that v is undefined, so an undefined pointer, v, is being dereferenced. Not i. Your version wouldn't even compile, since you're attempting v.i where v is a pointer. Commented Jan 10, 2019 at 19:41
  • 1
    @Hogan the -> dereferences v (which is a pointer) and not i (which is an integer). So the term nodehead->v->i is technically correct regarding the types (but the pointer variable member v is uninitialized, which leads to the crash) Commented Jan 10, 2019 at 19:55
  • @Hogan In general: When you write the expression p->x, then the "arrow" dereferences p, not x. Commented Jan 10, 2019 at 20:02

2 Answers 2

4

There is no memory allocated for the member vertex of your node. Either allocate memory for it like this:

nodehead->v = malloc(sizeof(vertex));

before assigning the member i, or - probably even better - change your struct node to already include a full instance of vertex

typedef struct Node {
    vertex v;
    struct Node *next;
} node;

and access i like this:

nodehead->v.i = 10;

The latter method has the advantage, that you do not need to manage the memory for two objects, but only for the node itself. OTOH, if your vertices are used independently from the linked list, this method may not be appropriate; it depends on the context.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I had just refactored my code and this was the issue that I didn't see at first.
0

The line nodehead->v->i = 10; is what is giving you your problems.

You are trying to assign a value to v, however, no memory for v has been allocated. A solution would be to allocate memory to v before reaching this line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.