0

I am not too sure why this code does not create a linked list with 5 nodes that each hold an integer value. I am currently getting a segfault and I commented on the line where the error is occurring. Any help would be appreciated, thank you.

struct node{
    int val;
    node * next;
}

int main(){
    node ** head;

    for(int i = 0; i < 5; i++){
        node * temp = new node;
        temp->val = i;

        (*head)->next = temp; //segfault here
    }
}
7
  • 2
    Why is is head a pointer to pointer in the first place? Commented Oct 5, 2016 at 23:19
  • Initializing head is probably a good idea rather than just using it. De-referencing an uninitialized pointer = crash. Commented Oct 5, 2016 at 23:20
  • What do you think (*head) will do? Commented Oct 5, 2016 at 23:26
  • head is pointer to pointer for ease and efficiency when creating my insert function later. Tadman, do you think that before the for loop I should be saying something like... head = new node; Commented Oct 5, 2016 at 23:27
  • 1
    head itself doesn't need to be a double pointer. You probably meant you want head to be a double pointer inside the body of insert: void insert(node** head, int data) so you can simply make head a single pointer inside main and pass its address: insert(&head, x). Commented Oct 5, 2016 at 23:40

4 Answers 4

1

head doesn't need to be a pointer to a pointer. It can simply be a pointer to another node which is always the first node in your linked list. head should also be initialized to null.

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

2 Comments

With my implementation it has to be, any ideas?
You can still use your double pointer, you just need to make sure head is pointing at something before you try to call (*head)->next, that's where your segfault is happening. Your code as-is-written would not produce a linked-list anyway, and needs further examination.
1

In your code, head is a double pointer. The pointer that head is pointing to does not have a constructed object and may be null, hence your segmentation fault.

I would recommend you keep head a simple pointer instead of a double pointer as well, since that is unnecessary.

2 Comments

Thanks for the reply wuoix, I need to use a double pointer for my implementation, any other ideas?
One problem to think about is unless your head is not always pointing to the first element, you'll never be able to properly construct the next element without losing elements you have created in the past.
0

Try this one, it should work. You still have to write code to de allocate memory.

struct node{
   int val;
   node * next;
};

int main(){
   node * head = new node();
   head->val = 999;
   node *curr=head;

   for(int i = 0; i < 5; i++) {
      node* temp = new node();
      temp->val = i;
      cout << "adding node " << i << endl;
      curr->next = temp; //segfault here
      curr = curr->next;
   }
   return 0;
}

2 Comments

Thank you for your reply Kemin Zhou I understand that just using a single pointer would work, but as for implementation of my insert function in regards to efficiency I need head initialized as a double pointer.
You mean head is an array of node pointers. You are already operating pointers without the double pointer, which should be very efficient.
0

The first thing you try to do in your program using "node **head" is dereference it, which, given that head is a pointer of uninitialized value, will most likely crash your program if you try to touch any data at that location. The proper thing to do would be to set *head = temp before trying to go to next.

Additionally, you do not need to initialize the list using a double pointer to take advantage of the double pointer insertion algorithm later. You can always say Node **head_pp = &head and go from there.

Right now, your code looks like this.

Node **head (stores anything since uninitialized)

*head
(uninitialized memory)

(*head)->next (tries to touch uninitialized memory address, OS kills program)

It should be noted that in most cases you will receive the segfault error, since most uninit memory holds 0's. Eventually you would see an access violation, though

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.