I'm traversing a list of nodes which have two fields: next and size. There comes a point in the code where I need to link in a new node and I am having trouble. I've found where the code seg faults and it looks like this. Note that curr is the current node in the list and I need to link in temp between curr and curr->next.
Node* temp = NULL;
temp = ((curr + 1) + a_memory_offset_int); //calculate the address where temp should go
temp->next = curr->next; //Seg faults here
temp->size = some_other_int; //Seg faults here as well
curr->next = temp;
Is there some way that I am trying to set the fields for a NULL node? Is there something wrong with the syntax (as I am confident the logic is correct)?
Without seeing more code, I suspect you might not understand what
a_memory_offset_int
is doing. It's doing the exactly same thing as the+ 1
, that is to say that it's doing pointer arithmetic. This:is equivalent to:
What you probably really want is:
Note the only difference is the multiplication of
a_memory_offset_int
bysizeof(Node)
. More simplified, this is what you want: