Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in memory block layout allocated by malloc and calloc?

Tags:

c

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?

like image 257
Avantika Sk Avatar asked Sep 19 '12 07:09

Avantika Sk


3 Answers

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)

like image 66
Jack Avatar answered Oct 19 '22 17:10

Jack


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.

like image 2
Art Avatar answered Oct 19 '22 19:10

Art


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

like image 1
Jeegar Patel Avatar answered Oct 19 '22 19:10

Jeegar Patel