Search code examples
cnulldynamic-memory-allocationcalloc

calloc() and NULL


I know that calloc allocates memory and writes zeroes to each cell, so my question is: is there a difference between using calloc or using malloc and running over the cells writing NULL to them? Are the zeroes of calloc equivalent to NULL?


Solution

  • No, they are not always equivalent, but on most popular machines you'll be fine. calloc writes a bit pattern of all-zeros to the allocated memory, but the null pointer value might not be all-bits-zero on some machines (or even just for some types on some machines).

    Check out the Null Pointers section of the C FAQ for lots and lots of information.