<?xml version="1.0" encoding="UTF-8"?>

<rdf:RDF 
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns="http://purl.org/rss/1.0/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:syn="http://purl.org/rss/1.0/modules/syndication/"
>

  <channel rdf:about="http://lwn.net/headlines/147014/">
    <title>LWN: Comments on "kzalloc()"</title>
    <link>http://lwn.net/Articles/147014/</link>
    <description>
This is a special feed containing comments posted
to the individual LWN article titled &quot;kzalloc()&quot;.

    </description>

    <syn:updatePeriod>hourly</syn:updatePeriod>
    <syn:updateFrequency>2</syn:updateFrequency>
    <items>
      <rdf:Seq>
	<rdf:li resource="http://lwn.net/Articles/147504/rss" />
	<rdf:li resource="http://lwn.net/Articles/147453/rss" />
	<rdf:li resource="http://lwn.net/Articles/147452/rss" />
	<rdf:li resource="http://lwn.net/Articles/147363/rss" />
	<rdf:li resource="http://lwn.net/Articles/147247/rss" />
	<rdf:li resource="http://lwn.net/Articles/147211/rss" />
	<rdf:li resource="http://lwn.net/Articles/147199/rss" />
	<rdf:li resource="http://lwn.net/Articles/147159/rss" />
	<rdf:li resource="http://lwn.net/Articles/147146/rss" />
      
      </rdf:Seq>
    </items>

  </channel>
    <item rdf:about="http://lwn.net/Articles/147504/rss">
      <title>calloc idiocy</title>
      <link>http://lwn.net/Articles/147504/rss</link>
      <dc:date>2005-08-14T23:03:55+00:00</dc:date>
      <dc:creator>chad.netzer</dc:creator>
      <description>
      Ahh, I see.  I did ignore the clearing aspect in my replies; I overlooked this as the the distinction that perhaps you (and bronson) were talking about.  But I hope all this does illustrate to bronson (if he or she is reading) that calloc() requires the &quot;nmemb&quot; argument for a reason, and that the overflow protection is easy to overlook, but an important feature of calloc().&lt;br&gt;
&lt;p&gt;
Ok, I understand your macro example now.  A compact way of doing the array allocation without the clear, and safer than malloc(n * sizeof(struct foo)).  Thanks for rewriting the declaration.&lt;br&gt;
&lt;p&gt;
Cheers.&lt;br&gt;
&lt;p&gt;
      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147453/rss">
      <title>calloc idiocy</title>
      <link>http://lwn.net/Articles/147453/rss</link>
      <dc:date>2005-08-14T02:50:57+00:00</dc:date>
      <dc:creator>giraffedata</dc:creator>
      <description>
      I think you missed the fundamental distinction of calloc() from malloc().  The difference in how you specify the size is incidental.  The reason calloc() exists is that it allocates memory that is set to zero (&quot;c&quot; is for &quot;clear&quot;), whereas malloc() allocates memory with arbitrary contents.
&lt;p&gt;
I'm not sure my macro example really did get garbled.  It looks fine on my screen, but does look a little funny because I tried to be consistent with the earlier example that used a one-character type name.  Let me try again:
&lt;p&gt;
&lt;pre&gt;
&lt;code&gt;
  struct foo * arrayOfFooStructs;
  MALLOCARRAY(arrayOfFooStructs, 4); 
&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;
It's supposed to be two lines.  The first one declares an array variable (technically a pointer, but practically an array) and the second allocates space for a 4-element array and assigns its address to that pointer.  I.e. it instantiates the array.
&lt;p&gt;
I have seen people use special &quot;multiply and malloc&quot; routines in order to deal with the arithmetic overflow while not taking the time to clear the memory as with calloc().  Also, these routines sometimes multiply more than two numbers together to get the size.

      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147452/rss">
      <title>calloc idiocy</title>
      <link>http://lwn.net/Articles/147452/rss</link>
      <dc:date>2005-08-14T02:26:34+00:00</dc:date>
      <dc:creator>chad.netzer</dc:creator>
      <description>
      Huh?  What would be the semantics of a &quot;hypothetical&quot; calloc(4 * sizeof(t)), and how would they be different from the malloc(4 * sizeof(t))?  Clearly bronson meant to compare malloc() to calloc()&lt;p&gt;
bronson's point was that if using malloc() and calloc() are almost equivalent, differing by just an internal multiplication, there would seem to be little need for calloc().  It is an explicit indication that you are allocating an array (as you stated), and he argued that this was less readable for him.&lt;p&gt;

But:&lt;p&gt;
&lt;pre&gt;
    malloc(n * sizeof(t))
&lt;/pre&gt;

and&lt;p&gt;

&lt;pre&gt;
   calloc(n, sizeof(t))
&lt;/pre&gt;
are NOT equivalent.  The calloc() is implemented as something like:&lt;p&gt;

&lt;pre&gt;
if (n &amp;amp;&amp;amp; ((n*sizeof(t))/n == n))
    p = malloc(n * sizeof(t));
else
    p = NULL;
&lt;/pre&gt;

Assuming I haven't made an error, this protects against n being large enough to cause an overflow (or, for example, when n is an int, and it's a small negative number).  The naive malloc() call could lead to serious complications, allocating a much smaller array than the caller expected.&lt;p&gt;

Your macro example got garbled, so I won't comment on it.&lt;p&gt;

      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147363/rss">
      <title>calloc idiocy</title>
      <link>http://lwn.net/Articles/147363/rss</link>
      <dc:date>2005-08-12T21:33:56+00:00</dc:date>
      <dc:creator>giraffedata</dc:creator>
      <description>
      &lt;blockquote&gt;
You, of course, meant to say &quot;malloc(4 * sizeof(t))&quot;, but we get your point.
&lt;/blockquote&gt;

&lt;p&gt;I'm pretty sure he meant what he wrote; it's what I would have written, anyway: an apples to apples comparison of two hypothetical designs for calloc(), one more readable than the other.

&lt;p&gt;I find calloc(4, sizeof(t)) easier to read.  It says rather explicitly that you're allocating space for 4 array elements, whereas calloc(4 * sizeof(t)) requires an extra mental step to go backwards through the arithmetic and say, &quot;Aha.  He's calculating how much memory a 4 element array would take.&quot;

&lt;p&gt;I myself do even better.  I use a macro thusly:

&lt;pre&gt;
&lt;code&gt;
    t * arrayOfT;
    MALLOCARRAY(arrayOfT, 4);
&lt;/code&gt;
&lt;/pre&gt;

      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147247/rss">
      <title>calloc idiocy</title>
      <link>http://lwn.net/Articles/147247/rss</link>
      <dc:date>2005-08-12T06:21:23+00:00</dc:date>
      <dc:creator>chad.netzer</dc:creator>
      <description>
      You, of course, meant to say &quot;malloc(4 * sizeof(t))&quot;, but we get your point.&lt;br&gt;
&lt;p&gt;
However, what happens when n * sizeof(t) is greater than size_t?  Then the request can succeed, due to overflow making a small number out of a very large number.  You get a pointer to the memory you requested, but you got a much smaller amount than you expected.  calloc() detects these errors and returns NULL.&lt;br&gt;
&lt;p&gt;
Of course, you may expect that these cases are rare, and modern OSes will simply memory fault when accessing unallocated memory.  However, on embedded systems, or kernel code, such memory protection may not exist, and this error can be much more problematic.  If a user can somehow trick the system into generating this allocation (ie. without checking every array memory request that a user might be able to influence for overflow), you have a potential security bug.  calloc() does exactly that, so it has its uses.&lt;br&gt;
&lt;p&gt;
See this lkml post for more info:&lt;br&gt;
&lt;p&gt;
&lt;a href=&quot;http://marc.theaimsgroup.com/?l=linux-kernel&amp;amp;m=112324048702317&amp;amp;w=2&quot;&gt;http://marc.theaimsgroup.com/?l=linux-kernel&amp;amp;m=112324...&lt;/a&gt;&lt;br&gt;
&lt;p&gt;
      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147211/rss">
      <title>calloc()</title>
      <link>http://lwn.net/Articles/147211/rss</link>
      <dc:date>2005-08-11T22:17:28+00:00</dc:date>
      <dc:creator>brettlevin</dc:creator>
      <description>
      I found a libc guru to ask.&lt;br&gt;
&lt;p&gt;
In 4.1BSD the args to malloc and calloc were unsigned ints.  There were at least some machines who had 16-bit ints, but which had more than 2^16 worth of addressable memory.  In such an environment it might be useful to allocate more than 64Kb in one call.&lt;br&gt;
&lt;p&gt;
Then POSIX changed these args to size_t, easing the size restriction.&lt;br&gt;
      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147199/rss">
      <title>calloc()</title>
      <link>http://lwn.net/Articles/147199/rss</link>
      <dc:date>2005-08-11T19:32:58+00:00</dc:date>
      <dc:creator>brettlevin</dc:creator>
      <description>
      Does anyone know the story behind the count parameter in libc's calloc()?&lt;br&gt;
      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147159/rss">
      <title>kzalloc()</title>
      <link>http://lwn.net/Articles/147159/rss</link>
      <dc:date>2005-08-11T15:01:43+00:00</dc:date>
      <dc:creator>nathan</dc:creator>
      <description>
      Let's not lose sight of the fact that the original two calls would still have been larger than the single kcalloc call --even with its useless parameter.&lt;br&gt;
      
      </description>
    </item>
    <item rdf:about="http://lwn.net/Articles/147146/rss">
      <title>calloc idiocy</title>
      <link>http://lwn.net/Articles/147146/rss</link>
      <dc:date>2005-08-11T13:53:45+00:00</dc:date>
      <dc:creator>bronson</dc:creator>
      <description>
      I have always thought that calloc/fwrite/etc's use of &quot;nmemb&quot; parameters was idiotic.  Which is easier to read?&lt;br&gt;
    calloc(4, sizeof(t))&lt;br&gt;
or&lt;br&gt;
    calloc(4 * sizeof(t))&lt;br&gt;
For me, the latter.  Especially with fwrite.  And it's smaller and more general too.&lt;br&gt;
&lt;p&gt;
Abolish nmemb!!&lt;br&gt;
&lt;p&gt;
      
      </description>
    </item>
</rdf:RDF>

