NULL v. zero
NULL v. zero
Posted Jul 15, 2004 15:52 UTC (Thu) by ikm (guest, #493)In reply to: NULL v. zero by dougm
Parent article: NULL v. zero
Thanks for the correction. Style coding standards may be more strict than the language itself, of course.
By the way, are there some real examples when the use of 0 instead of NULL would lead to some real error? I'm just curious.
Posted Jul 15, 2004 17:03 UTC (Thu)
by kamil (guest, #3802)
[Link] (4 responses)
int execl(const char *path, const char *arg, ...); Here, one should pass null pointer as the last argument. Passing 0 is usually OK on 32-bit machines, but not on 64-bit ones. You must explicitly cast 0 to a pointer type to conform to the standard. But NULL will be just as erroneous here, given that the standard allows it to be defined as a plain int 0. So you would in fact have to write (void*)NULL to conform both to the C standard and to Linus :-).
Posted Jul 15, 2004 19:25 UTC (Thu)
by knobunc (guest, #4678)
[Link] (1 responses)
-ben
Posted Jul 15, 2004 19:34 UTC (Thu)
by Ross (guest, #4065)
[Link]
Posted Jul 15, 2004 19:32 UTC (Thu)
by Ross (guest, #4065)
[Link]
Posted Jul 15, 2004 22:45 UTC (Thu)
by ikm (guest, #493)
[Link]
Of course, all other values for NULL would be incorrect, as it was explained in my initial posting. One can not define NULL as 0xFFFFFFFF, for example, as it would not qualify as a null pointer constant. Thanks for providing the example, that clarified things a bit!
0 is not good enough if the compiler can't guess from the context that you want a pointer. This typically happens in cases such as this one:NULL v. zero
Sorry, I don't see why you would need to cast NULL to conform to the spec as summarized above since it is says "implementation-defined null pointer constant" so it must provide something that is a 64-bit pointer.NULL v. zero
I think the other person mean (const char *)NULL, but maybe not.
NULL v. zero
Yeah, but unfortunately you can't pass plain NULL either (though it worksNULL v. zero
on almost any system). You have to cast it to the correct pointer type.
(Of course using 0 is even worse.)
I guess you can always safely pass NULL, as it would be declared as a plain 0 only in case it really physically is a plain 0. In any other case it would be ((void *)0). Any sane library would always declare NULL as ((void *)0), because it is the compiler that always knows the actual physical value for sure :)NULL v. zero