Q:

Is it better to use malloc () or calloc ()?

0

Is it better to use malloc () or calloc ()?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

The calloc function initialize the allocated memory with 0 but malloc don’t. So the memory which is allocated by the malloc has the garbage data. In another word you can say that calloc is equal to the combination of malloc and memeset.

See the below expression,

ptr = calloc(nmember, size);  //is essentially equivalent to
ptr = malloc(nmember * size);
memset(ptr, 0, (nmember * size));

Note: If you don’t want to initialize the allocated memory with zero, It would be better to use malloc over calloc.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

How can you determine the size of an allocated por... >>
<< What is the difference between malloc and calloc?...