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

Re: [PATCH 1/5] add SWAP macro

From
Jeff King <peff@peff.net>
Date
Jan 31, 2017, 22:36 UTC
Message-ID
<20170131223604.oa4h5ykhefvlergd@sigill.intra.peff.net>
In-Reply-To
<xmqqy3xqq2w0.fsf@gitster.mtv.corp.google.com>
On Tue, Jan 31, 2017 at 02:29:51PM -0800, Junio C Hamano wrote:
Show 26 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > ... I wonder if it would be more natural for it to take
> > pointers-to-objects, making it look more like a real function (i.e.,
> > SWAP(&a, &b) instead of SWAP(a, b)". And then these funny corner cases
> > become quite obvious in the caller, because the caller is the one who
> > has to type "&".
> 
> Hmmmm.  
> 
> While this looks very attractive in theory by forcing 'a' and 'b' to
> be lvalues, it probably invites mistakes go unnoticed during the
> review when the code wants to swap two pointer variables.  
> 
> For example,
> 
> apply.c:            SWAP(p->new_name, p->old_name);
> 
> is now a bug and will swap only the first byte of these names, and
> the correct way to spell it would become:
> 
> apply.c:            SWAP(&p->new_name, &p->old_name);
> 
> The latter clearly looks like swapping the new and old names, which
> is good, but I do not have any confidence that I will immediately
> spot a bug when presented the former under the new world order.

Yes, it's a problem with any function (or function-like interface) that takes an untyped pointer. You don't know if the caller meant the pointer or the pointer-to-pointer. In that sense it's no different than:

  memcpy(p->new_name, p->old_name, len);
Is that right, or did we mean to copy the pointers themselves?
It does also help the reverse case:
  int a, b;
  SWAP(a, b);

which would give an error (you forgot the "&"). I don't know which is more likely to be productive. But at least requiring pointer values makes it consistent with other functions like memcpy, etc.

-Peff
Previous: Junio C HamanoNext: Johannes Schindelin
Message 20 of 50 in “introduce SWAP macro”
  1. 0/5 introduce SWAP macroRené Scharfe, Jan 28, 2017
  2. 1/5 add SWAP macroRené Scharfe, Jan 28, 2017
  3. Johannes SchindelinJan 30, 2017
  4. René ScharfeJan 30, 2017
  5. Johannes SchindelinJan 30, 2017
  6. René ScharfeJan 30, 2017
  7. Johannes SchindelinJan 31, 2017
  8. René ScharfeJan 31, 2017
  9. Ramsay JonesFeb 1, 2017
  10. Johannes SchindelinFeb 1, 2017
  11. Johannes SchindelinJan 30, 2017
  12. René ScharfeJan 30, 2017
  13. Johannes SixtJan 30, 2017
  14. Johannes SchindelinJan 30, 2017
  15. René ScharfeJan 30, 2017
  16. Brandon WilliamsJan 30, 2017
  17. René ScharfeJan 31, 2017
  18. Jeff KingJan 31, 2017
  19. Junio C HamanoJan 31, 2017
  20. Jeff KingJan 31, 2017
  21. Johannes SchindelinFeb 1, 2017
  22. Jeff KingFeb 1, 2017
  23. René ScharfeFeb 1, 2017
  24. Junio C HamanoFeb 1, 2017
  25. René ScharfeFeb 7, 2017
  26. Junio C HamanoFeb 7, 2017
  27. Johannes SchindelinFeb 8, 2017
  28. Johannes SchindelinJan 31, 2017
  29. Jeff KingApr 24, 2017
  30. Jeff KingApr 24, 2017
  31. Duy NguyenApr 24, 2017
  32. René ScharfeApr 28, 2017
  33. Jeff KingApr 28, 2017
  34. René ScharfeApr 29, 2017
  35. Jeff KingApr 30, 2017
  36. René ScharfeMay 2, 2017
  37. 2/5 apply: use SWAP macroRené Scharfe, Jan 28, 2017
  38. 3/5 use SWAP macroRené Scharfe, Jan 28, 2017
  39. Johannes SchindelinJan 30, 2017
  40. René ScharfeJan 30, 2017
  41. Junio C HamanoJan 30, 2017
  42. René ScharfeJan 31, 2017
  43. 4/5 diff: use SWAP macroRené Scharfe, Jan 28, 2017
  44. Johannes SchindelinJan 30, 2017
  45. René ScharfeJan 30, 2017
  46. Junio C HamanoJan 30, 2017
  47. 5/5 graph: use SWAP macroRené Scharfe, Jan 28, 2017
  48. Johannes SchindelinJan 30, 2017
  49. René ScharfeJan 30, 2017
  50. Junio C HamanoJan 30, 2017

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.