Home > Net >  Segmentation fault while looping through a linked list
Segmentation fault while looping through a linked list

Time:01-13

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.

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;
}

CodePudding user response:

The problem of the program is that you did not initialize the pointer to the head node

node_t *tmp, *head;

As a result as new nodes are added tp the head the data member next of the last node has an indeterminate value.

You need to write

node_t *tmp, *head = NULL;

The function printList can look the following way

void printList( const node_t *head )
{
    for ( ; head != NULL; head = head->next )
    {
        printf( "%d\n", head->value );
    }
}

Also the function create_new_node should be defined the following way

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

    return next_pointer;
}

CodePudding user response:

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.

  •  Tags:  
  • Related