0

I have a server/client set up using sockets. The server constantly listens for client messages. If it detects a specific message, it should add a node to a linked list. For debugging purposes, I wanted to print out the entire linked list.

However, whenever I include the following code to traverse through the list:

if( listHead )
            {
                Node * searcherNode = (Node*)malloc(sizeof(Node));
                searcherNode = listHead;
                while( searcherNode->next != NULL )
                {
                    printf( "Account name i: %s\n", searcherNode->accountData.name );
                    searcherNode = searcherNode->next;
                }
                free( searcherNode );
            }

it begins to segfault as soon as any message is sent, not just the one that would run this code. The debugging printf right before this loop doesn't show up or anything, so it happens very early in the code.

Running if( searcherNode->next != NULL ) did not cause any errors, and neither did running searcherNode = searcherNode->next; on their own. Also, changing the while statement to

while( searcherNode != NULL )

didn't help either.

Any ideas why this segfault is occurring? Thank you so much!

Edit: Same issue with this code:

Node * searcherNode = listHead;
                while( searcherNode != NULL )
                {
                    printf( "Account name i: %s\n", searcherNode->accountData.name );
                    searcherNode = searcherNode->next;
                }

Struct as defined in header file:

typedef struct bigNode
{
    struct bigNode *next;
    BankAccount accountData;  
} Node;

Relevant code in main file:

Node *listHead = NULL; //in global declarations

Node creation:

Node * nodeBuilder;
nodeBuilder->accountData = accountBuilder;
nodeBuilder->next = listHead;

listHead = nodeBuilder;
  • Just to point out something you might want to be aware of: C is a language in which you can have "time traveling bugs". If you invoke [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior), all bets are off...including that you might start getting crashes in perfectly valid code that appears to be chronologically prior to the place where you made the mistake. And it wouldn't be considered a bug in the compiler--they aren't required to guarantee a program with any undefined behavior anywhere act valid anywhere else. – HostileFork says dont trust SE Nov 30 '18 at 09:13
  • Could you please show how your struct is defined? In general is better to provide an [mcve], something we could test on our own. – Bob__ Nov 30 '18 at 09:28
  • Did that, and will do that in the future – Tim S Pazda Nov 30 '18 at 09:39
  • The posted snipped for node "creation" doesn't allocate any memory for the node itself. Is that what you have in your actual code? – Bob__ Nov 30 '18 at 09:44
  • Don't use printf for debugging, use fprintf and print to stderr. fprintf(stderr, "my string"); – Andrew Zacharakis Nov 30 '18 at 09:48
  • `Node * nodeBuilder;` followed by `nodeBuilder->accountData` is invalid. The pointer `nodeBuilder` doesn't point anywhere, it's value is *indeterminate* (and seemingly random), and dereferencing the pointer will lead to *undefined behavior*. – Some programmer dude Nov 30 '18 at 09:52
  • I fixed that using `malloc` but I'm still getting the issue. – Tim S Pazda Dec 04 '18 at 04:19

1 Answers1

0

First of all, you shouldn't really cast the result of malloc. Secondly, that malloc creates a memory leak since you immediately make the pointer variable searcherNode point somewhere else.

And that reassignment is probably what makes your program crash: Since searcherNode will not point to the memory you allocated with malloc, then the call to free will attempt to free something which should not be free'd. In fact, this will attempt to free the last node in the list, without removing it from the list.

If the memory for that last node was not allocated with malloc then you can't pass the pointer to free as that will lead to undefined behavior. If the memory was allocated with malloc, then your next attempt to dereference the last (now free'd) node will also lead to undefined behavior.

The simple solution: Don't call malloc and most importantly don't call free.


What you do with

Node * searcherNode = malloc(sizeof(Node));
searcherNode = listHead;

is in a way similar to

int myValue = 10;
myValue = 5;

and then wondering why myValue is not equal to 10.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Maybe it should also be pointed out that the allocation is useless in the first place. OP is traversing the list to print its content. – Bob__ Nov 30 '18 at 09:14
  • I appreciate that detailed description, as I'm sure you could tell I'm pretty new to C and malloc is going right over my head. However I tested it without malloc as shown in the edit, but still get the same issue – Tim S Pazda Nov 30 '18 at 09:22
  • @TimSPazda Then the problem is most likely how you create your list. Please try to show us an [mcve], including the creation of a list (and of course exhibits the problem you have). – Some programmer dude Nov 30 '18 at 09:26