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

Open 1 Answers 29 Views Technology
Is it better to use malloc () or calloc ()?

1 Answer

0

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.

by (280k points)
...