Possibly dereferencing NULL in C

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:

temp = ((curr + 1) + a_memory_offset_int);

is equivalent to:

temp = (Node*)(((char *)curr + 1*sizeof(Node)) + a_memory_offset_int*sizeof(Node));

What you probably really want is:

temp = (Node*)(((char *)curr + 1*sizeof(Node)) + a_memory_offset_int);

Note the only difference is the multiplication of a_memory_offset_int by sizeof(Node). More simplified, this is what you want:

temp = (Node*)((char *)curr + a_memory_offset_int) + 1;

A linked list node's memory address isn't really important - you shouldn't be calculating it yourself, and instead calling malloc, then linking it in.

Something more like this:

Node* temp = NULL;
temp = malloc(sizeof(Node)); // create a new node, allocating fresh memor

temp->next = curr->next;     // should be fine now...
temp->size = some_other_int;

curr->next = temp;

Sample program for verification of pointer arithmetic.

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int size;
    struct node * next;
} Node;

int main() {
    void *memory = malloc(10*sizeof(Node));//allocate of Node * 10
    Node *curr = memory;
    Node *temp = NULL;

    temp = curr + 1;
    temp->size = 999;
    printf("%lu\n", sizeof(Node));
    printf("%p,%p\n", (void*)curr, (void*)temp);
    //00000000004D67B0,00000000004D67C0 <- difference is 16
    int a_memory_offset_int = 16;
    temp = curr + a_memory_offset_int;
    if(temp > &curr[9])//curr[9] is last element
        printf("It is outside the allocated memory\n");//this display
    temp = curr + a_memory_offset_int/sizeof(Node);
    printf("%d\n", temp->size);//999

   return 0;
Tags: C / Null / Linked List

Similar questions

What's the difference between cout<<cout and cout<<&cout in c++?
This might be a beginner question and understanding how cout works is probably key here. If somebody could link to a good explanation, it would be great. cout<<cout and cout<<&cout print hex values separated by 4 on a linux x86 machine.
Difference between 'cout<<cout' and 'cout<<&cout' in c++?
I want to know actual difference between cout<<cout and cout<<&cout in c++? In my compiler cout<<cout returns 0x477864 & cout<<&cout returns 0x477860 at any time.It shows it has 1 digit of difference between them.What are the significance of these?
Why does 'std::endl' require the namespace qualification when used in the statement 'std::cout << std::endl;", given argument-dependent lookup?
I was looking at the Wikipedia entry on argument-dependent lookup, and (on Jan 04, 2014) the following example was given: ... with the following comment: Note that std::endl is a function but it needs full qualification, since it is used as an argument to operator<< (std::endl is a function pointer, not a function call). My thought is that th...
after delete endl in cout << "hello" << endl, my C++ program stopped working
This is my code. This code can run well. But when I delete "endl" in "cout << "Hello world!" << endl;", It can't run.This is what I get when delete endl The following is the code that deleted endl.
what is the diffrence between std::cout<< x ; and std::cout<<x<<std::endl;?
I'm a newbie to programming, I started teaching myself yesterday, I've been getting everything but I honestly, do not understand the difference between and Nobody has explained this to me, and I'm asking to stay on the safe side.
Does anyone know which relation may exist between registration-free COM and drag/drop functionality?
Does anyone know which relation may exist between registration-free COM and drag/drop functionality? Specifically, we have a huge C++ CAD/CAM application comprising a number of EXEs and several hundreds DLLs. Many of them serve as COM servers (both in-proc and out-of-proc) and/or clients, and also implement ActiveX controls. The most of ActiveX con...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.