10

calloc allocates num blocks of memory, each of size size:

void * calloc ( size_t num, size_t size );

Allocate space for array in memory Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.

In contrast, malloc allocates one block of memory of size size:

void * malloc ( size_t size );

Allocate memory block Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.


Is there any difference between both (except for the zero-initialization by calloc)?

What does calloc means exactly by num blocks of memory as in practice the returned memory region is contiguous as well.

I believe there has to be some difference, otherwise it wouldn't make much sense to define two different interfaces for these methods?

Community
  • 1
  • 1
Avantika Sk
  • 103
  • 1
  • 5
  • yes, that is why I did not even try thinking that the memory is non-contiguous. So does that mean that terminology, n blocks of memory is misguiding? – Avantika Sk Sep 19 '12 at 07:57
  • @AvantikaSk Yes, that's misguiding. – nos Sep 19 '12 at 08:01
  • 1
    Actually calloc allocates a single block of memory suitable to store an array of elements of size n. On most implementations calloc calls malloc and then zeroes the memory. I think they have two different prototypes by accident and they are different only because calloc zeroes the memory. – Analog File Sep 19 '12 at 08:16
  • @moooeeeep : thanks for re-framing my question. – Avantika Sk Sep 19 '12 at 08:16
  • 1
    possible duplicate of [c difference between malloc and calloc](http://stackoverflow.com/questions/1538420/c-difference-between-malloc-and-calloc) – Bo Persson Sep 19 '12 at 08:42

7 Answers7

4

Sorry, no differences (but the memory is zeroed)

Calloc() can be useful when used with arrays, where you have two different values in hand: the size of arrays and the dimension of one single cell. The returned area memory is contiguous in both cases, and they probably use the same data structures for keeping track of such area (but this is implementation dependent)

Jack
  • 1,488
  • 11
  • 21
  • If I use calloc(4,1), can I assign value to all 4 bytes in one instance? like : char *ptr = (char *)calloc(4,1); *ptr = is it possible and safe to do this? – Avantika Sk Sep 19 '12 at 08:02
  • Is it possible and safe if returned pointer is not null: you are guaranteed that the allocated area is at least 4 bytes. Remember: never cast the return value of calloc/malloc (in C). – Jack Sep 19 '12 at 08:08
  • Plus: if you want to assign a value to the allocated area do not use calloc which is slower than malloc (due to the inizialization). Use calloc only if you really need a zero inizialization – Jack Sep 19 '12 at 08:11
2

In practice they do the same thing. The advantage of calloc is that good implementations will perform an overflow detection when doing the multiplication needed to determine how much memory you need.

If you do it something like this:

void *
calloc(size_t nmemb, size_t size)
{
    size_t sz = nmemb * size;
    void *res = malloc(sz);

'sz' might not end up being what you expect it to be. malloc will then allocate much less than the caller expected, but the caller can end up treating the returned area as large enough. This leads to heap overflows will all the security implications that usually has.

Art
  • 19,807
  • 1
  • 34
  • 60
1

i have tried to find out how calloc works

i find below code

/* We use this function occasionally since the real implementation may
   be optimized when it can assume the memory it returns already is
   set to NUL.  */
void * weak_function
calloc (size_t nmemb, size_t size)
{   
  /* New memory from the trivial malloc above is always already cleared.
     (We make sure that's true in the rare occasion it might not be,
     by clearing memory in free, below.)  */
  size_t bytes = nmemb * size;

#define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
  if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
      && size != 0 && bytes / size != nmemb)
    return NULL;

  return malloc (bytes);
}

You can see there is no big difference between calloc and malloc.

you can brouse code of glibc at here

http://sourceware.org/git/?p=glibc.git;a=blob;f=elf/dl-minimal.c

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
0

The only real difference is the 0 initialization with calloc().

Why calloc() works with block size and block count while malloc() doesn't, has never been clear to me.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • What does this answer? Whatever the answer says is already mentioned in the Q by the OP. – Alok Save Sep 19 '12 at 07:57
  • 1
    @Als It answers "How are these memory different apart form the calloc's "0" initialized memory?" with "The only real difference is the 0 initialization with `calloc()`.", acknowledging that there is no other difference. – glglgl Sep 19 '12 at 07:58
0

They are both exactly the same. Calloc wants you to specify how many blocks of a given size you want to allocate, because it initializes them with 0. Underlying structure is exactly the same.

tu_ru
  • 267
  • 1
  • 7
0

You can think of calloc() as being pretty much:

void* calloc(size_t nmemb, size_t size)
{
  const size_t nbytes = nmemb * size;
  void *p = malloc(nbytes);
  if(p != NULL)
    memset(p, 0, nbytes);
  return p;
}

Note that it might be implemented completely differently, but it is the functional equivalent of the above. There's no difference in the "internal layout" of the memory, you get a contigous block.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

Yes .What Calloc() does is it allocates n block each of size size_t size & then each block of size size_t size to ZERO .

Bharat Jain
  • 177
  • 1
  • 16