I\'ve read with interest the post C difference between malloc and calloc. I\'m using malloc in my code and would like to know what difference I\'ll have using calloc instead.
Assuming the total amount of memory being initialized in your two examples is the same, allocating the memory with calloc()
might be faster than allocating the memory with malloc()
and then zeroing them out in a separate step, especially if in the malloc()
case you zero the elements individually by iterating over them in a loop. A malloc()
followed by a memset()
will likely be about as fast as calloc()
.
If you do not care that the array elements are garbage before you actually store the computation results in them, there is no need to actually initialize your arrays after malloc()
.