Calloc () and NULL

I know that calloc allocates memory and writes zeros to each cell, so my question is, is there a difference between using calloc or using malloc and running over cells that write NULL? Are calloc zeros equivalent to NULL?


source to share

2 answers

No, they are not always equivalent, but on the most popular machines everything will be fine. calloc

writes a bit pattern of all zeros to the allocated memory, but the null pointer value may not be completely bit-zero on some machines (or even for some types on some machines).

Check out the Null Pointers section in the C FAQ for a lot of information.



NULL does not guarantee that all bits are set to 0, even if it is always compared to the integer constant 0.

Calloc will set all bits to 0 just like calling memset. It is acceptable that the resulting value (s) will not be compared to NULL.

Therefore, they are not equivalent.



All Articles