3 ms·
Yup, potential performance issues were the first things that came to my mind on seeing that code too. Plus, it's just silly to say that someone that just wrote
by demallien 14y ago
Yup, potential performance issues were the first things that came to my mind on seeing that code too. Plus, it's just silly to say that someone that just wrote a linked list doesn't understand pointers - pretty much by definition they do understand pointers, otherwise their linked list isn't going to work.
My personal rule of thumb concerning pointers is that two-stars should never be seen except in the case where you want a function to be able to modify a pointer that was passed in as a parameter. That's it. If I get handled a module that has two-stars in it under any other circumstance, I refactor it. I do this because:
a) lots of people find it difficult to thing about pointers to pointers (although I've never understood why empirically I note that this is the case).
b) Having to dereference two pointers to get to the information that you want is not just slow because of extra work needing to be done, but also because you've just increased the need for doing a page change in memory, which on some architectures can be surprisingly expensive.
- to3m 14y agoWhere does the double dereference come from in the second case? curr->next and (* curr) both perform a dereference. (If anything, (* curr) will be at least as efficient, if not more, because there's guaranteed to be no offset.) (Does anybody know how you type an asterisk?)
- demallien 14y agoMost CPU architectures offer special optimised instructions for grabbing data in a structure - it's done as an offset from the base pointer. A double star dereference can not benefit from this optimisation.
- to3m 14y agoI think there are the same number of dereferences in both cases? One to fetch the next pointer, and one to update the previous next pointer if a removal is required. You could probably make the second bit of code worse by not holding the value of star curr in entry.
- deleted 14y ago[deleted]
- deleted 14y ago[deleted]
- gsg 14y ago* * curr Unfortunately the spaces seem to be unavoidable.