9

I was wondering whether calloc() is preferable to a malloc followed by a memset. The latter appears to be the most common way of allocating and initializing memory.

A github code search turns up many calloc test and implementations but in the first number of pages no code actually using calloc.

Does anyone who knows of any projects/organizations that use or recommend using calloc and the circumstances where recommend it?

From the comments and answers below, here's some the thoughts that have emerges so far:

  • calloc(n, size) can prevent overflow that is possible with malloc(n * size)

  • combining malloc and memset gives calloc a chance to request a page that is known to already be zeroed.

  • a disadvantage to calloc that the combined steps may preclude other wrappers around malloc.

Kevin Cox
  • 3,080
  • 1
  • 32
  • 32
  • 2
    Never known anyone to use it, and I've never used it either despite being aware of its existence. I don't know why but it just doesn't feel like C. – Dave Mar 18 '13 at 00:51
  • 6
    Note that `malloc()` followed by `memset()` is practically guaranteed to be slower than `calloc()`. – Dietrich Epp Mar 18 '13 at 00:58
  • If you search GitHub for `malloc`, you also find implementations and test code before you find use. – Dietrich Epp Mar 18 '13 at 01:03
  • @DietrichEpp regarding your `malloc/memset` vs `calloc` speed comment: I'd be surprised if that's got any truth to it when using an optimising compiler. Can you give a reason or show a benchmark? All I can think of is better caching, but malloc doesn't traverse the memory in the first place. – Dave Mar 18 '13 at 02:07
  • 2
    @Dave: It has nothing to do with the compiler. The function call itself is faster. See my answer to another question: http://stackoverflow.com/questions/2688466/why-mallocmemset-slower-than-calloc/2688522#2688522 — the speed difference can be enormous, I measured a difference of 1500x in one benchmark. – Dietrich Epp Mar 18 '13 at 02:15
  • 1
    @DietrichEpp: wow, fair enough. I think I'll be using calloc when I need zeroed memory from now on! – Dave Mar 18 '13 at 02:19
  • @DietrichEpp I find malloc useage on the second page. I'm not saying calloc is unused but it does appear rarer. – Kevin Cox Mar 18 '13 at 04:26
  • @KevinCox: Could you provide a link? I only see code for testing `malloc()` on the second page. And I'm sure `calloc()` is rarer, just like division is rarer than multiplication, but you don't see people asking why we can't get rid of it. – Dietrich Epp Mar 18 '13 at 05:22
  • @DietrichEpp It looks like my vetting process wasn't good enough. You are correct. Something to note that github appears prefer files with the search term in the name, effectively making this simple benchmark useless. – Kevin Cox Mar 18 '13 at 14:36
  • I'm voting to re-open this one because 1) it is constructive to learn the reasons behind current practices and 2) I believe that on operating systems, there is in-fact difference between what malloc/memset and calloc. – Raymond Hettinger Aug 08 '15 at 02:35
  • 1
    There is another difference between them: integer overflow. `malloc(num * size)` and `calloc(num, size)` behave differently for large numbers. For more details see [Ray Lai’s Undeadly article on integer overflows](http://lteo.net/blog/2014/10/28/reallocarray-in-openbsd-integer-overflow-detection-for-free/). Also because of this, OpenBSD created a [`reallocarray`](http://lteo.net/blog/2014/10/28/reallocarray-in-openbsd-integer-overflow-detection-for-free/) function. – Cristian Ciupitu Aug 08 '15 at 03:15
  • 1
    @RaymondHettinger, it wouldn't hurt to rephrase the question to make it more appropriate for this site. – Cristian Ciupitu Aug 08 '15 at 03:25

1 Answers1

15

Well, I use calloc in quite a bit of C code, so I guess that's an answer. I think the slightly unusual call method (number of elements and size of element) may throw people. However, one other reason why you may not see as many calls as you would expect is that a lot of larger projects use wrappers around malloc, calloc, and friends that do error handling (usually terminating the program) on memory allocation failure. So the actual code uses xcalloc instead.

One reason to use calloc over malloc plus memset is that calloc may be more efficient. If the C library already knows that a page is zeroed (perhaps it just got new zeroed memory from the OS), it doesn't have to explicitly zero it.

rra
  • 3,807
  • 18
  • 29