Fault in Complex Memory Allocation and Segmentation menu

User Tag List

Results 1 to 3 of 3
  1. #1
    YreiauW's Avatar Active Member
    Reputation
    19
    Join Date
    Jul 2022
    Posts
    9
    Thanks G/R
    2/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Fault in Complex Memory Allocation and Segmentation

    I'm working on a C program that includes sophisticated memory allocation and dynamic data structures, and I'm getting a segmentation error that's been bothering me for a long, so I read this article but couldn't go very far. Can someone assist me? Here's a condensed version of my code:
    Code:
    #include  <stdio.h>
    #include  <stdlib.h>
    
    struct Node {
        int data;
        struct Node* next;
    };
    
    struct LinkedList {
        struct Node* head;
    };
    
    void initializeLinkedList(struct LinkedList* list) {
        list->head = NULL;
    }
    
    void insertNode(struct LinkedList* list, int value) {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        newNode->data = value;
        newNode->next = list->head;
        list->head = newNode;
    }
    
    void displayList(struct LinkedList* list) {
        struct Node* current = list->head;
        while (current != NULL) {
            printf("%d -> ", current->data);
            current = current->next;
        }
        printf("NULL\n");
    }
    
    int main() {
        struct LinkedList myList;
        initializeLinkedList(&myList);
    
        insertNode(&myList, 10);
        insertNode(&myList, 20);
        insertNode(&myList, 30);
    
        displayList(&myList);
    
        return 0;
    }
    When I execute the program, I get a segmentation fault. The code is meant to generate a linked list and show its contents, but something is obviously incorrect. I assume it has something to do with memory allocation, but I'm having problems identifying the problem. Can you assist me in identifying the issue and suggesting a solution?

    Fault in Complex Memory Allocation and Segmentation
  2. #2
    Razzue's Avatar Contributor Avid Ailurophile

    CoreCoins Purchaser Authenticator enabled
    Reputation
    379
    Join Date
    Jun 2017
    Posts
    588
    Thanks G/R
    186/268
    Trade Feedback
    2 (100%)
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by YreiauW View Post
    I'm working on a C program that includes sophisticated memory allocation and dynamic data structures, and I'm getting a segmentation error that's been bothering me for a long, so I read this article but couldn't go very far. Can someone assist me? Here's a condensed version of my code:
    Code:
    #include  <stdio.h>
    #include  <stdlib.h>
    
    struct Node {
        int data;
        struct Node* next;
    };
    
    struct LinkedList {
        struct Node* head;
    };
    
    void initializeLinkedList(struct LinkedList* list) {
        list->head = NULL;
    }
    
    void insertNode(struct LinkedList* list, int value) {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        newNode->data = value;
        newNode->next = list->head;
        list->head = newNode;
    }
    
    void displayList(struct LinkedList* list) {
        struct Node* current = list->head;
        while (current != NULL) {
            printf("%d -> ", current->data);
            current = current->next;
        }
        printf("NULL\n");
    }
    
    int main() {
        struct LinkedList myList;
        initializeLinkedList(&myList);
    
        insertNode(&myList, 10);
        insertNode(&myList, 20);
        insertNode(&myList, 30);
    
        displayList(&myList);
    
        return 0;
    }
    When I execute the program, I get a segmentation fault. The code is meant to generate a linked list and show its contents, but something is obviously incorrect. I assume it has something to do with memory allocation, but I'm having problems identifying the problem. Can you assist me in identifying the issue and suggesting a solution?
    I'm not super familiar with cpp, but is it possible malloc is failing in 'insertNode' and causing that issue? šŸ¤” or the linked list isn't initialized properly?
    "May all your bacon burn"

  3. #3
    Hazzbazzy's Avatar wannabe hackerlol Authenticator enabled
    Reputation
    1335
    Join Date
    Aug 2011
    Posts
    1,206
    Thanks G/R
    243/484
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Razzue View Post
    I'm not super familiar with cpp, but is it possible malloc is failing in 'insertNode' and causing that issue? šŸ¤” or the linked list isn't initialized properly?
    I'm not personally able to replicate the error. As Razzue says there is no error handling for the malloc return pointer, but it could also be a subroutine within malloc that is failing or it could be referencing corrupt memory; are you able to provide more details on the error?
    "HOLY TIME MACHINE BATMAN! it's 1973!"
    https://youtube.com/Hazzbazzy

Similar Threads

  1. Request Mac Editing Guide in placing the file, and Gnome Male ---> Undead Male
    By Bourbonkills in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 08-21-2007, 02:05 AM
  2. Photos of Me in Emerald Dream (1) and my GM Video
    By eldavo1 in forum Screenshot & Video Showoff
    Replies: 1
    Last Post: 06-14-2007, 04:28 PM
  3. Mount up in Black Rock Mountain and more
    By Matt in forum World of Warcraft Exploits
    Replies: 0
    Last Post: 03-25-2006, 05:16 AM
All times are GMT -5. The time now is 04:31 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search