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
Here is the relevant code to get the last element , to add, and to count
So here is how I call the function :
Please help me spot where I went wrong?12 Replies
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
@steffen
Thank you, that makes a lot of sense! So, if I initialize list to NULL before calling addLL like this:
This is what you mean?
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?
The latter
it points to a random location.
its indeterminate when you declare a value but dont initialize it.
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
It applies to variables and structures
basically uninitialized items will lead to undefined behavior, you don't know what the compiler will do.
Initialize your variables. and you start from a known point.
Thanks for clarifying @ZacckOsiemo
No worries glad to have been of service.