I'm implementing a singly linked list using C.
struct Node
{
int nodeValue;
struct Node* pNext;
};
struct Node* head;
void createList()
{
head = (struct Node*)malloc(sizeof(struct Node));
}
void insertNodeAtBeginning(int value)
{
struct Node* newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->nodeValue = value;
struct Node* auxNode;
if(head->pNext == NULL)
{
head->pNext = newNode;
}
else
{
auxNode = head->pNext;
head->pNext = newNode;
newNode->pNext = auxNode; //breakpoint set here
}
}
I've set a breakpoint on line marked by the comment. The value of auxNode is non-NULL:
(gdb) p auxNode
$4 = (struct Node *) 0x5555555551db <createList+18>
However the value of newNode->pNext to which the auxNode is assigned is NULL:
(gdb) p newNode->pNext
$5 = (struct Node *) 0x0
Could anyone clarify this behavior? Thanks.
stepto run that line and then examine the values again.createListallocates space for one node and setsheadto point to it but does not fill in that space, nor does any of the other code shown.insertNodeAtBeginningmay be using the contents of that space uninitialized. (c) When the code breaks at the linenewNode->pNext = auxNode, the line has not been executed yet.head->pNextis not initialized increateList