Thread: "Segmentation fault" when returning the extracted node from a LIFO linked list

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    11

    "Segmentation fault" when returning the extracted node from a LIFO linked list

    I'm starting to learn to work with linked lists. Specifically, I'm trying to make a LIFO (Last In, First Out) linked list. I made different functions to insert a node, to print the whole list, to free the allocated memory of every node... There's also a global variable called root that points to the first node of the list. Here's the code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
        int data;
        struct node *next;
    };
    
    struct node *root = NULL;
    
    void InsertNode(int number) {
        struct node *NewNode;
        NewNode = malloc(sizeof(struct node));
        NewNode->data = number;
    
        if (root == NULL) {
            NewNode->next = NULL;
            root = NewNode;
        }
        else {
            NewNode->next = root;
            root = NewNode;
        }
    }
    
    void PrintList(void) {
        struct node *CurrentNode = root;
        while (root != NULL) {
            printf("%i\n", CurrentNode->data);
            CurrentNode = CurrentNode->next;
        }
    }
    
    int ExtractFirstNode(void) {
        if (root != NULL) {
            int extracted = root->data;
            struct node *NodeToBeFreed = root;
            root = root->next;
            free(NodeToBeFreed);
            return extracted;
        }
        else {
            return -1;
        }
    }
    
    void FreeWholeList(void) {
        struct node *CurrentNode = root;
        struct node *NodeToBeFreed;
        while (CurrentNode != NULL) {
            NodeToBeFreed = CurrentNode;
            CurrentNode = CurrentNode->next;
            free(NodeToBeFreed);
        }
    }
    
    int main(void) {
        InsertNode(6);
        InsertNode(4);
        InsertNode(8);
        InsertNode(7);
        printf("Complete list:\n");
        PrintList();
        printf("\n");
        printf("Extracting the first node: %i\n", ExtractFirstNode());
        FreeWholeList();
        return 0;
    }
    There's also a function called ExtractFirstNode() that just frees the first node. It returns the number of the extracted node, and is called in the main() function to see what number was storing the first node which has been freed. I'll paste the code of the function in question:
    Code:
    int ExtractFirstNode() {
        if (root != NULL) {
            int extracted = root->data;
            struct node *NodeToBeFreed = root;
            root = root->next;
            free(NodeToBeFreed);
            return extracted;
        }
        else {
            return -1;
        }
    }
    If the list is empty it will return -1, which will be used as an error code. Like I said, the number it returns is used in the main() function to inform the user the first number that has been freed:
    Code:
    int main(void) {
        InsertNode(6);
        InsertNode(4);
        InsertNode(8);
        InsertNode(7);
        printf("Complete list:\n");
        PrintList();
        printf("\n");
        printf("Extracting the first node: %i\n", ExtractFirstNode());
        FreeWholeList();
        return 0;
    }
    But the function is being quite problematic, because it doesn't return the number of the first node. Instead the program's output is the following:
    Code:
    Complete list:
    7
    8
    4
    6
    Segmentation fault
    
    Process returned 139 (0x8B)   execution time : 0.001 s
    Press ENTER to continue.
    Why is it not printing the recently freed node's data?
    Thanks in advance.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,690
    It's your print list which is blowing up because of the faulty loop.
    while (root != NULL)

    Use the variable you're using to step through the list.

    Learn to use the debugger. It would have pointed this out right away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2018
    Posts
    11
    Quote Originally Posted by Salem View Post
    It's your print list which is blowing up because of the faulty loop.
    while (root != NULL)

    Use the variable you're using to step through the list.

    Learn to use the debugger. It would have pointed this out right away.
    You are right. Silly me! I don't even know why I used the root variable, I did it right on the FreeWholeList() function.
    Thank you very much Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-28-2016, 02:47 AM
  2. Replies: 5
    Last Post: 04-10-2012, 02:31 PM
  3. Segmentation fault when using fopen("filename","w+")
    By Marslakoo in forum C Programming
    Replies: 6
    Last Post: 11-21-2011, 08:15 AM
  4. Replies: 2
    Last Post: 10-31-2011, 11:57 AM
  5. [Segmentation Fault] fopen("filename","w")
    By gibbofresco in forum C Programming
    Replies: 7
    Last Post: 07-04-2009, 04:32 AM

Tags for this Thread