by nemetroid · 5y ago · 0 comments
remove_list_entry(entry)
{
prev = NULL;
walk = head;
// Walk the list
while (walk != entry) {
prev = walk;
walk = walk->next;
}
// Remove the entry by updating the
// head or the previous entry
if (!prev)
head = entry->next;
else
prev->next = entry->next;
}
remove_list_entry(entry)
{
// The "indirect" pointer points to the
// *address* of the thing we'll update
indirect = &head;
// Walk the list, looking for the thing that
// points to the entry we want to remove
while ((*indirect) != entry)
indirect = &(*indirect)->next;
// .. and just remove it
*indirect = entry->next;
} while (!!walk && walk != entry) {
prev = walk;
walk = walk->next;
}
or while (!!indirect && !!(*indirect) && (*indirect) != entry)
indirect = &(*indirect)->next;
...because without those checks it's pretty easy to see where you'll crash... but by putting in those checks, perhaps the second method might actually be less 'elegant'.Edit: did I miss something? Re-checking this, the second method will still crash if/when next is NULL (because you can't get the address of NULL), which means the 'elegant' seems to be a quagmire of lurking faults.
If you call that method and the element doesn't exist, presumably something has gone wrong already. Adding a NULL pointer check is not going to fix it. You just silently ignore the error. You'll prevent the crash, but there's no mechanism for handling the error.
But for general purpose code, e.g. libraries where performance isn't super important but stability is, more defensive programming would be a thing to do.
At the very least, the code should add comments on the the assumptions that are vital...otherwise n00bs will copy the code as gospel and run into all sorts of problems.
while (!!(*indirect) && (*indirect) != entry)
indirect = &(*indirect)->next;
Also I would rather use * indirect instead of !!(* indirect).Reminds me of the editor(s) who helped "fix" bukowski's poems
Similarly, I’m not a fan of everything Linus wrote, but I wouldn’t enforce bad CS101 code on him so that little Jimmy could read it, and I like Linux.
https://www.pbs.org/newshour/arts/poetry/bukowksis-poems-wer...
this line has 4 spaces in front of it.
What Hacker news could use instead are quote blocks. Now people often use the code blocks as quote blocks. This has no superfluous indentations. remove_list_entry(entry)
{
for (p = &head; *p; p = &(*p)->next)
{
if (*p == entry)
{
*p = entry->next;
break;
}
}
}
However, what the choice does objectively impact is performance. Being able to assume the object exists allows you to avoid checking against NULL, which removes instructions in the loop. I'd guess they anticipate this code might be called from performance-critical code, and that might be why they coded it like this.Branch predictors will probably do pretty well on this, but still.
The purpose of the OP and the Linux's talk is to show a better way to walk through linked list, not to show a full implementation.
Usually variable names in a loop is the object being manipulated not the address of that pointer which means p isn't a pointer to the object which makes it completely confusing.
If the article used pp which is the typical way you describe a pointer to pointer it'd help
// The "indirect" pointer points to the
// *address* of the thing we'll update
The "indirect" pointer points to the thing we'll update. See at the bottom, it's updating *indirect, so "indirect" points to the thing being updated. On the other hand, "indirect" points to the address of the thing we'll remove. There's a specific item being removed, and there's a specific thing that will be updated; the thing being updated comes immediately before the thing being removed; they're not the same thing.I don't think so. I think the 'indirect pointer' points to the previous 'next' (or the head). It doesn't point to the address of the previous 'next' (or the head). What you say is adding an additional level of indirection that doesn't exist.
Reality:
indirect -> previous next -> first element
What you're saying:
indirect -> address of previous next -> previous next -> first element
In reality indirect contains the address of the previous next, but it doesn't point to the address of the previous next.
What does "holds" mean? If holds means actually contains the memory, then I don't see the distinction between "thing" and "entry". They both refer to the exact same piece of memory.
In most cases you use a linked list like this, you don't care about the order of things, just that you can iterate over all items in the list.
Would you find the comments in the second version helpful, or should they also be removed?
Edit: let me lay my cards on the table. If the comments really are necessary, it doesn’t seem elegant. I’m pro-comments, but that’s because not all code can be readable and elegant all the time.
So I'll disagree with the notion that only illegible or unreadable code needs comment.
I've been C/C++ for about 25 years.
So at a function level the comments is giving a highlevel description to the reader as to the functionality contained, and at the line level comments exist only to say describe the programmers intentionality, why it was implemented this way rather than some other way.
I have generally found (there are always exceptions) that if you find yourself needing "what" comments within a function you should be considering breaking it into smaller pieces.
I've been being paid to write c/c++ for about 25 years
// indirect is either &head or &(node->next), so *indirect is equivalent to "next node"
Someone who knows their C should be able to figure it out in a few minutes by just reading the code.For example if the linked list was:
&next: (100, 0xFFFF)
0xFFFF: (101, 0xDEFF)
0XDEFF: (102, 0)
Then calling remove on head would leave us with:
&next: (101, 0xDEFF)
0xFFFF: (101, 0xDEFF)
0XDEFF: (102, 0)
(It would be on the caller to free the memory address 0XFFFF)
(Granted, the code in the original TED talk is broken, as it doesn't define "head".)
A linked list in C isn't a struct with a "head"; "head" is just a pointer pointing to the head element.
The OP article should be retracted.
But it should be retracted? Really?