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;