git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: gitk problems: can't unset "idinlist(...)"

From
Linus Torvalds <torvalds@linux-foundation.org>
Date
Jul 21, 2007, 06:47 UTC
Message-ID
<alpine.LFD.0.999.0707202335570.27249@woody.linux-foundation.org>
In-Reply-To
<20070721062448.GA8722@coredump.intra.peff.net>
On Sat, 21 Jul 2007, Jeff King wrote:
Show 6 quoted lines
> 
> By which I mean you can keep the 'for' loop, since in either case we are
> always reading p->next (if we skip, then we set *pp to p->next anyway,
> and if we don't, then pp becomes a pointer to p->next).
> 
> So this is a more readable patch which I believe is equivalent.
Fair enough, that also works.

Anyway, just because this is actually something I've noticed a lot of people do wrong, I actually do tend to think that when you traverse a singly-linked list and remove entries, you should *never* traverse the list itself, you should only ever traverse the "pointer to the pointer".

So the difference between your version and mine is that the
	while ((p = *pp) != NULL)

model really only has one single "iterator variable": "pp". Which makes it a lot easier to think about and avoid problems.

In contrast, your for-loop actually keeps track of *two* independent variables, the "tail pointer of the list we're building up" (pp) and the "head pointer of the old list we're traversing" (p).

Does it work? Yes. But I tend to prefer the "there's a single list" model, where you just remove entries as you traverse it through a single indirect pointer.

They're certainly equivalent, but slightly different mindsets. I at least personally find the "single pointer" less likely to have problems, because you can never get out of sync (which was basically exactly the problem that Junio's code had), because there is nothing to get out of sync _with_ when you just have a single main iterator variable.

			Linus
Previous: Jeff KingNext: Jeff King
Message 10 of 15 in “gitk problems: can't unset "idinlist(...)"”
  1. Linus TorvaldsJul 20, 2007
  2. Jeff KingJul 21, 2007
  3. Linus TorvaldsJul 21, 2007
  4. Junio C HamanoJul 21, 2007
  5. Jeff KingJul 21, 2007
  6. Jeff KingJul 21, 2007
  7. Linus TorvaldsJul 21, 2007
  8. Jeff KingJul 21, 2007
  9. Jeff KingJul 21, 2007
  10. Linus TorvaldsJul 21, 2007
  11. Jeff KingJul 21, 2007
  12. pointer to pointer (was: gitk problems: can't unset "idinlist(...)")David Kastrup, Jul 21, 2007
  13. David KastrupJul 21, 2007
  14. Linus TorvaldsJul 21, 2007
  15. Paul MackerrasJul 21, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.