Tuesday, September 13, 2011

What is the difference between 'malloc()' and 'calloc()'?

malloc():

  • creates the single block (a block of memory in bytes) of given size by user.
  • initializes the reserved memory block to garbage value.
  • takes only one argument.
calloc():
  • creates multiple blocks of given size.
  • initializes the reserved memory block to zero.
  • calloc takes two arguments.
int *p;
p=(int*) malloc(sizeof(int)*5);
p=(int*) calloc(5, sizeof(int)*5);

No comments :