News for hackers. Stuff that matters.
Re:Calloc or Malloc? Any thoughts? by earle on Wednesday August 13, 2008 @11:42AM (#275064)
Calloc clears the memory to all bits zero. All bits zero doesn't necessarily mean zero values. Not many people will make this distinction and happily write broken code thinking that calloc always clears the values to zero accordingly.

calloc's zero fill is all-bits-zero, and is therefore guaranteed to yield the value 0 for all integral types, and '\0' for character types -- but does not guarantee useful null pointer values or floating-point zero values.

Some C implementations represent null pointers by special, nonzero bit patterns when it can be arranged so that inadvertently using those values triggers automatic hardware traps.

Re:Calloc or Malloc? Any thoughts? by yan on Wednesday August 13, 2008 @12:58PM (#275145)
I'm just curious, but can you point me to an implementation of C that doesn't treat (void*)0 as NULL?
Re:Calloc or Malloc? Any thoughts? by qwph on Wednesday August 13, 2008 @01:32PM (#275181)
( void* )0 is NULL, in that NULL is (most usually) #defined to be ( void* )0. But the literal 0 in a pointer context doesn't guarentee that the null pointer is actually all bits zero.

To put it another way, if you have a literal zero in a pointer context, the compiler will replace it with the machine's representation of a null pointer, which may or may not be a machine word with all bits zero.

I think that's right...

Re:Calloc or Malloc? Any thoughts? by ConradHex on Wednesday August 13, 2008 @02:27PM (#275262)
You are theoretically correct.

But: can you name a platform where a null pointer is not represented by zeros in binary?

Re:Calloc or Malloc? Any thoughts? by orib on Wednesday August 13, 2008 @04:39PM (#275464)