4 ms·
The double-pointer trick makes for a more concise code, but it doesn't always lead to the code that is faster. That said, it's actually an excellent C intervie
by huhtenberg 14y ago
The double-pointer trick makes for a more concise code, but it doesn't always lead to the code that is faster.
That said, it's actually an excellent C interview question. Combined with "write a binary tree iterator" it reliably measures one's exposure to thoughtful C programming.
- lucian1900 14y agoIt likely is faster because it avoids more branching altogether, thus branch mis-predictions.
- ww520 14y agoBut memory access is more expensive. Double de-referencing a pointer to pointer might perform worse.
- stephencanon 14y agoThere is actually no difference in the memory access pattern of the two approaches, unless your compiler is entirely out to lunch. Memory is only touched in either approach to: a. loading the address of the next node. b. updating the link address when a node is deleted. which are performed the same number of times on the same underlying data in either approach.
- kabdib 14y agoSee above comment about load-hit-stores. Memory order and what's in flight in the pipeline matter in surprising ways.
- mtoddh 14y agoThat said, it's actually an excellent C interview question This was actually one of the very first questions we used to ask during interviews at Tenable. I'm honestly kind of amazed it's now seen as some sort of tricky technique. I would think (or hope) most C programmers would see it as basic competence with pointers.
- huhtenberg 14y ago"Hope" is more like it. I went to one interview where they asked to traverse a list, and since it was for a senior C position I gave them the * * version. There was this pause and then their lead dev said that this cannot possibly work. It was really awkward.
- mtoddh 14y agoWow. I tell you- reading through some of the responses on here, I'm feeling a lot more confident about my job security as a C programmer.
- npsimons 14y agoI would think (or hope) most C programmers would see it as basic competence with pointers. Which is exactly what Torvald's was saying. All the comments here going "that's unreadable" or "it's too clever" scare me. And I don't even consider myself some C coding rockstar (took me a bit to sort out my own mental picture of pointers).