Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Segmentation fault while looping through a linked list

I’m just learning about linked lists and I tried to create a function, that loops through a linked list and prints out every value.

However, at the end of my while-loop, when I past the last node I got an segmentation fault.

I though I would get rid of the segmentation fault by setting the condtion from (temp != NULL) to (temp->next != NULL), but still I got the error.
I would appriciate some help here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I provide the print function in which I get the error, but I also could provide the complete code if needed.

void printList(node_t *head){
    node_t *temp = head;

    if (temp != NULL) {
        while (temp->next != NULL) {
            int i = temp->value;
            printf("%d\n", i);
            temp = temp->next;
            
        }
    }
}

Added some more code.

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

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

typedef struct node node_t;

void printList(node_t *head){
    node_t *temp = head;

    if (temp != NULL) {
        while (temp->next != NULL) {
            int i = temp->value;
            printf("%d\n", i);
            temp = temp->next;

        }
    }

}

node_t *create_new_node(int value){
    node_t *next_pointer = malloc(sizeof(node_t));
    next_pointer->value = value;
    next_pointer->next = NULL;
    return next_pointer;
}

node_t *insert_at_head(node_t **head, node_t *new_node){
    new_node->next = *head;
    *head = new_node;
    return new_node;
}

int main(){
    node_t *tmp, *head;

    for(int i= 0; i<25; i++){
        tmp = create_new_node(i);
        insert_at_head(&head, tmp);
    }
    
    printList(head);
    
    return 0;
}

>Solution :

Initialise head:

node_t *tmp, *head=NULL;

That way what ends up being the very last next is well defined and will prevent access who-knows-where.

Then I get a nice backward output (minus the last element, first inserted, the 0; see comment by Jabberwocky).

E.g. here https://www.tutorialspoint.com/compile_c_online.php

Otherwis you do:

head->OhNo
head->1->OhNo
head->2->1->OhNo
….

This will trip up your output function, when it arrvives at/before the "OhNo", which points who-knows-where.

With init it is
head->NULL
head->1->NULL
head->2->1->NULL

The latter works cleanly with your output function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading