Malloc or Calloc and when

Possible Duplicate:
c difference between malloc and calloc

Is calloc same as malloc with memset?? or is there any difference

char *ptr;
ptr=(char *)calloc(1,100)

  or

char *ptr;
ptr=(char *) malloc(100);
memset(ptr,0,100);


This is how calloc is defined by gcc:

PTR
calloc (size_t nelem, size_t elsize)
{
  register PTR ptr;

  if (nelem == 0 || elsize == 0)
    nelem = elsize = 1;

  ptr = malloc (nelem * elsize);

  if (ptr) bzero (ptr, nelem * elsize);

  return ptr;
}

http://gcc.gnu.org/viewcvs/trunk/libiberty/calloc.c?view=markup

with

void
bzero (void *to, size_t count)
{
  memset (to, 0, count);
}

As result, it's the same .

Both are allocating memory and then set it to 0

链接地址: http://www.djcxy.com/p/28456.html

上一篇: realloc和malloc函数

下一篇: Malloc或Calloc以及何时