Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Like Linus example, this is just an example too and does not cover or comment on all the issues, so there are still elephants lurking in the shadows, just to name a few:

- Can entry be NULL? (segfault)

- Can head be NULL (list is empty)? (segfault)

Since the Linus example has the apparent precondition that the entry to be removed must be present in the list, there are no segfaults with proper API use. Since your code tries to eliminate that precondition, I'd argue "Remove NULL from the list" and "Remove something from an empty list" is something to expect, but you didn't account for that.

Point being: Don't be overly critical of examples; they are just examples.



I guess my fundamental criticism is this: how is a list traversal that doesn't check for the end of list being held up as an example of "good taste"?

You're right that they're all just examples. I'm just a random guy on the internet, and it's easy to push back against me. But we all have the tendency to nod along when someone we respect says something sagely on TED. It seemed useful to point out that the sage has no clothes on in this case. So go ahead, point at my skimpy cladding as well. I won't mind.


I can't see why the code from akkartik would segfault when entry is NULL? What am I missing?


If entry and head are null, segfault on line 4.

If entry is null and head is valid segfault on line 10.

--edit--

As pointed out by my esteemed colleagues below, I didn't read the loop conditional properly. Only the first fault applies


> If entry is null and head is valid segfault on line 10.

To get to line 10, we need prev->next == entry. Since we are talking about the case where entry == NULL, that requires prev->next == NULL

The for loop condition is prev->next, which will fail if prev->next == NULL, hence line 10 would never be reached in that case.

The difficulty of analysing all of this shows why mutable structures and variables make code hard to follow - perhaps the real good taste solution is to use something like finger trees instead of linked lists, together with a language which eliminates null.


How can you reach line 10 when entry is NULL?

(prev->next==entry) can only be true at the end of the list, but the for-condition already checks that.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: