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

Re: [PATCH 00/15] Rearrange xcalloc arguments

From
Jeff King <peff@peff.net>
Date
May 28, 2014, 01:16 UTC
Message-ID
<20140528011658.GA17688@sigill.intra.peff.net>
In-Reply-To
<20140526233741.GB8570@hudson.localdomain>
On Mon, May 26, 2014 at 04:37:41PM -0700, Jeremiah Mahler wrote:
Show 13 quoted lines
> > xcalloc takes two arguments: the number of elements and their size.
> > The vast majority of the Git codebase passes these arguments in the
> > correct order, but there are some exceptions. This patch series
> > corrects those exceptions.
> > 
> 
> Let me see if I understand the issue underlying this patch set.
> 
> xcalloc works like calloc and takes two arguments, the number of
> elements and the size of each element.  However, many calls specified
> these arguments in the reverse order.  It didn't produce a compile
> error because both arguments are the same type.  And it didn't produce
> a run time error because A*B is the same as B*A.
Yes, I think that is a good summary.

It may be theoretically possible that an implementation of calloc() can use the distinction between the two arguments to adjust the padding or alignment of the result. However, I don't know if any implementation actually does this, or if it is even true in theory. You can find some discussions[1,2] online, but nothing conclusive. The most plausible theory I saw is that early K&R C may have done something clever here, but ANSI C alignment requirements effectively remove any wiggle room for the implementation.

But it certainly does not hurt to follow the spec and be consistent.
-Peff
[1] http://stackoverflow.com/questions/501839/is-calloc4-6-the-same-as-calloc6-4
[2] https://groups.google.com/d/msg/comp.lang.c/jZbiyuYqjB4/NIAmeNd11IoJ
Previous: Jeremiah Mahler
Message 28 of 28 in “Rearrange xcalloc arguments”
  1. 00/15 Rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  2. 01/15 builtin/add.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  3. Jeremiah MahlerMay 26, 2014
  4. Brian GesiakMay 27, 2014
  5. Jeremiah MahlerMay 27, 2014
  6. Eric SunshineMay 27, 2014
  7. Brian GesiakMay 27, 2014
  8. Eric SunshineMay 27, 2014
  9. Junio C HamanoMay 27, 2014
  10. Jeremiah MahlerMay 28, 2014
  11. Brian GesiakMay 28, 2014
  12. 02/15 builtin/ls-remote.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  13. 03/15 builtin/remote.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  14. 04/15 commit.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  15. 05/15 config.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  16. 06/15 diff.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  17. 07/15 hash.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  18. 08/15 hash.h: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  19. 09/15 http-push.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  20. 10/15 imap-send.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  21. Jeremiah MahlerJun 10, 2014
  22. 11/15 notes.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  23. 12/15 pack-revindex.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  24. 13/15 reflog-walk.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  25. 14/15 remote.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  26. 15/15 transport-helper.c: rearrange xcalloc argumentsBrian Gesiak, May 26, 2014
  27. Jeremiah MahlerMay 26, 2014
  28. Jeff KingMay 28, 2014

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.