Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6982883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:20:48+00:00 2026-05-27T18:20:48+00:00

I dont have explansion for what happening to my code, i have a generic

  • 0

I dont have explansion for what happening to my code, i have a generic linked list
and it worked like a charm just yesterday, i didn’t change it in any way, but somehow
now when the getters and setters of the list try to reach the fields of the list or it’s nodes i getting those SIGSEGV faults and my code terminating, that’s the relevant part of my list.c file :

struct LinkedList
{
    Node m_head;
    Node m_tail;
    int m_numOfElements;
};


List CreateLinkedList()
{

    List list = (List)malloc(sizeof(struct LinkedList));

    if (list == NULL)
    {
        printf("memory alloc failed\n");
        return NULL;
    }
    list->m_head = NULL;
    list->m_tail = NULL;
    list->m_numOfElements = 0;

    return list;
}

i also have that typedef in the list.h file which included in the ilst.c file:

typedef struct LinkedList* List;

after debugging for hours i noticed that the head and tail dont get NULL value when the list created, they still have some address instead of zeros, it also happens in the create node and other uses of this list, why is it? my heap is corrupted? why it doesn’t assign null to those fields as it should?! anyway plz remember it worked well and i have no idea what changed it.. thanks!

edit:

this is the AddNode function in my node.c file:

int AddNode(List list, Element data)
{
    Node newNode;
    //there is no linked list to add the element to
    if(list == NULL)
        return -1;

    newNode = CreateNewNode(data);

    if(newNode == NULL)
    {
        printf("Failed allocating memory\n");
        return 1;
    }

    //Empty Linked List
    if (list->m_head == NULL)
    {
        list->m_tail = list->m_head = newNode;
    }
    else
    {
        SetNext(list->m_tail,newNode);
        SetPrev(newNode, list->m_tail);
        list->m_tail = newNode;
    }
    //increase num of elements
    list->m_numOfElements++;
    return 1;
}

here on the first call to AddNode, when the list is empty and I’m adding the first element
it jumps the if of empty linked list and go for else that come after that, it shouldn’t. what can make this happen in my app code?

edit 2:

ok this is the only struct allocation before i use the list:

struct Server_t
{
    List UsersList;
};

Result CreateServer(Server thisServer)
{
    if (thisServer = (Server)malloc(sizeof(struct Server_t)))
    {
        thisServer->UsersList = CreateLinkedList();
        return Success;
    }
    return Failed;
}

and the main call it that way:

Server mainFacebookServer;
CreateServer(mainFacebookServer);

Result is enum, and Server is a pointer to that struct.

edit 3:

Node CreateNewNode(Element data)
{
    Node newNode = (Node)malloc(sizeof(struct NODE));

    newNode->m_next = NULL;
    newNode->m_prev = NULL;
    newNode->m_data = data;
    return newNode;
}

and the setters are:

void SetNext(Node node, Node toSet)
{
    node->m_next = toSet;
}
void SetPrev(Node node, Node toSet)
{
    node->m_prev = toSet;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:20:49+00:00Added an answer on May 27, 2026 at 6:20 pm

    This code does not do what you think it does:

    Result CreateServer(Server thisServer)
    {
        if (thisServer = (Server)malloc(sizeof(struct Server_t)))
        {
            thisServer->UsersList = CreateLinkedList();
            return Success;
        }
        return Failed;
    }
    

    If you’re going to return the Server by value, you must pass a pointer into the function:

    Result CreateServer(Server *thisServer)
    {
        if (*thisServer = (Server)malloc(sizeof(struct Server_t)))
        {
            (*thisServer)->UsersList = CreateLinkedList();
            return Success;
        }
        return Failed;
    }
    

    The original code both leaks memory and invites segmentation faults because the struct Server_t value used after CreateServer() returns has not been initialized.


    Working Code

    The following code works under valgrind (there’s system-allocated memory still reachable, but nothing leaked or abused). The problem was indeed in the CreateServer() function, as diagnosed earlier.

    #include <stdlib.h>
    #include <stdio.h>
    #include <assert.h>
    
    typedef int Element;
    
    typedef struct Node *Node;
    struct Node
    {
        Node    m_prev;
        Node    m_next;
        Element m_data;
    };
    
    struct LinkedList
    {
        Node m_head;
        Node m_tail;
        int m_numOfElements;
    };
    
    typedef struct LinkedList *List;
    
    struct Server_t
    {
        List UsersList;
    };
    typedef struct Server_t *Server;
    typedef enum Result { Failed, Success } Result;
    
    extern Result CreateServer(Server *thisServer);
    extern Result AddNode(List list, Element data);
    extern void SetPrev(Node node, Node toSet);
    extern void SetNext(Node node, Node toSet);
    extern Node CreateNewNode(Element data);
    extern List CreateLinkedList(void);
    extern void DestroyServer(Server server);
    extern void DestroyLinkedList(List list);
    
    List CreateLinkedList(void)
    {
        List list = (List)malloc(sizeof(struct LinkedList));
    
        if (list == NULL)
        {
            printf("memory alloc failed\n");
            return NULL;
        }
        list->m_head = NULL;
        list->m_tail = NULL;
        list->m_numOfElements = 0;
    
        return list;
    }
    
    Node CreateNewNode(Element data)
    {
        Node newNode = (Node)malloc(sizeof(struct Node));
    
        newNode->m_next = NULL;
        newNode->m_prev = NULL;
        newNode->m_data = data;
        return newNode;
    }
    
    void SetNext(Node node, Node toSet)
    {
        node->m_next = toSet;
    }
    
    void SetPrev(Node node, Node toSet)
    {
        node->m_prev = toSet;
    }
    
    Result AddNode(List list, Element data)
    {
        Node newNode;
        //there is no linked list to add the element to
        if (list == NULL)
            return Failed;
    
        newNode = CreateNewNode(data);
    
        if (newNode == NULL)
        {
            printf("Failed allocating memory\n");
            return Failed;
        }
    
        //Empty Linked List
        if (list->m_head == NULL)
        {
            list->m_tail = list->m_head = newNode;
        }
        else
        {
            SetNext(list->m_tail,newNode);
            SetPrev(newNode, list->m_tail);
            list->m_tail = newNode;
        }
        //increase num of elements
        list->m_numOfElements++;
        return Success;
    }
    
    Result CreateServer(Server *thisServer)
    {
        if ((*thisServer = (Server)malloc(sizeof(struct Server_t))) != 0)
        {
            if (((*thisServer)->UsersList = CreateLinkedList()) != 0)
                return Success;
        }
        return Failed;
    }
    
    void DestroyLinkedList(List list)
    {
        Node node;
        Node next;
        assert(list != 0);
        for (node = list->m_head; node != 0; node = next)
        {
            next = node->m_next;
            free(node);
        }
        free(list);
    }
    
    void DestroyServer(Server server)
    {
        assert(server != 0);
        assert(server->UsersList != 0);
        DestroyLinkedList(server->UsersList);
        free(server);
    }
    
    int main(void)
    {
        Server mainFacebookServer;
        if (CreateServer(&mainFacebookServer) == Success)
        {
            if (AddNode(mainFacebookServer->UsersList, 1) == Success)
            {
                /* Use new user list */
            }
            DestroyServer(mainFacebookServer);
        }
        return(0);
    }
    

    When compiled with the XCode 4.x GCC/LLVM on MacOS X 10.7.2 and run with Valgrind 3.7.0, I get:

    $ /usr/bin/gcc -O3 -g -std=c99 -Wall -Wextra -Werror xxx.c -o xxx 
    $ valgrind --leak-check=full xxx 
    ==51464== Memcheck, a memory error detector
    ==51464== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
    ==51464== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
    ==51464== Command: xxx
    ==51464== 
    ==51464== 
    ==51464== HEAP SUMMARY:
    ==51464==     in use at exit: 2,095 bytes in 32 blocks
    ==51464==   total heap usage: 35 allocs, 3 frees, 2,151 bytes allocated
    ==51464== 
    ==51464== LEAK SUMMARY:
    ==51464==    definitely lost: 0 bytes in 0 blocks
    ==51464==    indirectly lost: 0 bytes in 0 blocks
    ==51464==      possibly lost: 0 bytes in 0 blocks
    ==51464==    still reachable: 2,095 bytes in 32 blocks
    ==51464==         suppressed: 0 bytes in 0 blocks
    ==51464== Reachable blocks (those to which a pointer was found) are not shown.
    ==51464== To see them, rerun with: --leak-check=full --show-reachable=yes
    ==51464== 
    ==51464== For counts of detected and suppressed errors, rerun with: -v
    ==51464== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 1 from 1)
    $
    

    When run with --show-reachable=yes, the extra reports are similar to this one (from a different run, as you can see:

    ==51375== 616 bytes in 7 blocks are still reachable in loss record 8 of 8
    ==51375==    at 0xC3F3: calloc (vg_replace_malloc.c:569)
    ==51375==    by 0x312AAA: _xpc_calloc (in /usr/lib/system/libxpc.dylib)
    ==51375==    by 0x313384: _xpc_base_create (in /usr/lib/system/libxpc.dylib)
    ==51375==    by 0x319CE2: xpc_string_create (in /usr/lib/system/libxpc.dylib)
    ==51375==    by 0x318EF5: xpc_dictionary_set_string (in /usr/lib/system/libxpc.dylib)
    ==51375==    by 0x31AF49: _libxpc_initializer (in /usr/lib/system/libxpc.dylib)
    ==51375==    by 0x18E7D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
    ==51375==    by 0x7FFF5FC0FD19: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
    ==51375==    by 0x7FFF5FC0FA65: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
    ==51375==    by 0x7FFF5FC0D257: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
    ==51375==    by 0x7FFF5FC0D1F0: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
    ==51375==    by 0x7FFF5FC0E02A: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
    ==51375== 
    

    Not exciting – except for the amount of background work going on – and definitely an issue (or not) with the system rather than the code in the program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i dont have microsoft access but would like to open an mdb file, is
Not a question but i dont have a blog and i have just created
1.i dont have iphone, so may i know does iphone come with pre-installed flash
Some people want to see the DB structure and dont have VS, is there
How can I dynamically load a usercontrol in ASP.NET if I dont have access
What does x :: xs' mean? I dont have much functional experience but IIRC
I'm playing around with cookies. And I dont have any cookies called PHPSESSID. Do
I have a TreeView in my form for which I dont want to have
I have a data structure which uses composite ids (Which I dont wish to
I dont get it. I have used a similar/same approach for many years now,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.