disturbing issue I'm having with LinkedList in C language, when I count how many nodes should be

Hello world!! @Middleware & OS There is this disturbing issue I'm having with LinkedList in C language, when I count how many nodes should be in the list, I always get 1
LL count: 1
LL count: 1
Here is the relevant code to get the last element , to add, and to count
void addLL(LL * head)
{
LL *newNode;
LL *tail = getLastNode(head);

newNode = malloc(sizeof(LL));
if(newNode != DEF_NULL)
{
newNode->ID=-1;
newNode->TCB=-1;
newNode->next = DEF_NULL;

if(!head) head = newNode;
else tail->next = newNode;
}
}

LL * getLastNode(LL * head)
{
LL *temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
}
return temp;
}

CPU_INT32U countLL(LL * head)
CPU_INT32U countLL(LL * head)
{
CPU_INT32U elements = 0;
LL * temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
elements++;
}
return elements;
}
void addLL(LL * head)
{
LL *newNode;
LL *tail = getLastNode(head);

newNode = malloc(sizeof(LL));
if(newNode != DEF_NULL)
{
newNode->ID=-1;
newNode->TCB=-1;
newNode->next = DEF_NULL;

if(!head) head = newNode;
else tail->next = newNode;
}
}

LL * getLastNode(LL * head)
{
LL *temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
}
return temp;
}

CPU_INT32U countLL(LL * head)
CPU_INT32U countLL(LL * head)
{
CPU_INT32U elements = 0;
LL * temp = head;
while(temp->next != DEF_NULL)
{
temp = temp->next;
elements++;
}
return elements;
}
So here is how I call the function :
LL* list;

addLL(list);
int count = countLL(list);
Debug_LOG("LL count: %i", count);
LL* list;

addLL(list);
int count = countLL(list);
Debug_LOG("LL count: %i", count);
Please help me spot where I went wrong?
12 Replies
steffen
steffen7mo ago
First thing I see is that you do not initialize LL* list to NULL. This means it points to a random address in memory. And that is why the if(!head) check does not work as you would expect
Marvee Amasi
Marvee Amasi7mo ago
@steffen Thank you, that makes a lot of sense! So, if I initialize list to NULL before calling addLL like this:
LL* list = NULL;
addLL(list);
LL* list = NULL;
addLL(list);
This is what you mean?
Marvee Amasi
Marvee Amasi7mo ago
So @steffen I am curious to know how a non-null list before the first addLL call affects the behavior? Does it create an empty list or point to some unintended memory location?
ZacckOsiemo
ZacckOsiemo7mo ago
The latter
ZacckOsiemo
ZacckOsiemo7mo ago
it points to a random location.
ZacckOsiemo
ZacckOsiemo7mo ago
its indeterminate when you declare a value but dont initialize it.
Marvee Amasi
Marvee Amasi7mo ago
Okay @ZacckOsiemo , Are there any common pitfalls I should be aware of when working with linked lists in C, especially regarding memory management? @steffen @Middleware & OS @ZacckOsiemo
ZacckOsiemo
ZacckOsiemo7mo ago
It applies to variables and structures
ZacckOsiemo
ZacckOsiemo7mo ago
basically uninitialized items will lead to undefined behavior, you don't know what the compiler will do.
ZacckOsiemo
ZacckOsiemo7mo ago
Initialize your variables. and you start from a known point.
Marvee Amasi
Marvee Amasi7mo ago
Thanks for clarifying @ZacckOsiemo
ZacckOsiemo
ZacckOsiemo7mo ago
No worries glad to have been of service.
Want results from more Discord servers?
Add your server