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