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

Showing Answers 1 - 6 of 6 Answers

baseersd

  • Sep 18th, 2007
 

1) malloc() does not initialize the memory allocated. Hence garbage will be present
    calloc() initializes the allocated memory with 0.

2) malloc() requires 2 arguments in the form of ( data type and number of variables of     such     data type).
    calloc() requires only one argument in the form of (total number of bytes)

  Was this answer useful?  Yes

paul.anuj

  • Aug 5th, 2008
 

malloc() and calloc() both are use to allocate memory.

malloc takes only one argument that is number of memory bytes, mostly we use sizeof() operator, which allocates the memory of that  number of bytes

whereas calloc uses two argument that is *number of bytes and **number of blocks of that much bytes in memory.
example;
int x=malloc(sizeof(int));
int x=calloc(sizeof(int),6);
mostly we use user defined datatypes such as structure or union,but for simplicity i have used integer datatype.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions