In the load function, there is line where I'm assigning a pointer to another. Here is a snippet of the code used

    // Declaration  node *header[26];


    some code  


    // Here is the line where i get a segmentation error 
    pointer = header[cursor]->next;

After using gdb It turns out that the line pointer = header[cursor]->next return a SIGSEGV... I tried to initialize to NULL or to hard code a value like 0 instead of cursor.. But I have the same problem!

UPDATE 1-Cursor is an int, and pointer is a node* pointer/ 2-I've checked cursor using gdb, and it contains a valid value (0)/ 3-After the declaration, all the header[i]->next are initialized to NULL/ 4-Nothing has been malloced at header[cursor]

  • Sounds like you're trying to access something that doesn't exist. How are cursor and pointer declared? What does the structure of node look like? Have you confirmed that cursor contains a valid value when this line is executed? Does header[cursor]->next contain a valid address, or NULL, or has it even beein initialized? Was there anything malloc'd at header[cursor]?
    – Cliff B
    Jun 26 '15 at 22:51
  • @CliffB i've updated the topic with the information... Speaking about the fourth point which is malloc, do I need to allocate memory for header[cursor] even if I did node* header[26] ? Thank you for your help
    – Ahmed
    Jun 26 '15 at 23:05

Based on your updates and answers to my question this is what's happening.

If nothing has been malloc'd for header[cursor], then header[cursor]->next doesn't exist. header[cursor] either contains whatever junk was in that memory location when it was created, or, NULL, if it was initialized as such. Trying to access it will absolutely generate a segmentation fault because you are trying to access something that just doesn't exist.

I have a problem with what you said in the update though. You said that all of the header[]->next values were initialized to NULL, but you also said that you never malloc'd any memory to header[cursor], so you couldn't initialize ->next because it doesn't exist. You may have initialized the header[] array so that all of the pointers in the array were set to NULL, but that's not the same as setting any elements of a node structure that an array element may point at. You would have had to malloc the node, thus generating and assigning the address of the node to header[x], and then initialize the elements inside the node.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

  • Thank you @Cliff B
    – Ahmed
    Jun 27 '15 at 0:16

it seems to me that you're confused thinking that defining an array of pointers would allocate memory automatically for these pointers to point to. afraid this is not true. when having something like

foo *arr[SIZE];

where foo * is a pointer type, this allocates memory for SIZE pointers (i.e., to store the addresses of the memory locations that they'll point to). this doesn't allocate memory for these pointers to point to.

if you want any of these pointers to point to somewhere on the heap, you have to allocate memory for that pointer using a function like malloc, calloc, etc. example:

// allocate memory for a foo and point to it by foo[10]
arr[10] = malloc(sizeof(foo));

the segmentation fault is probably caused because header[cursor] doesn't have memory allocated for it (i.e., doesn't point to anything).

  • Thank you @Kareem
    – Ahmed
    Jun 27 '15 at 0:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .