On Jun 17, 2009, at 12:32 AM, Michael Schwingen wrote:
NULL is for pointers, nothing else. If you want to test if something is- If you are testing that a variable is non-NULL, write that. Similarly for a test for NULL. Taking the short-cut just makes it less clear what you are doing and why. Besides, it isn't necessarily guaranteed that NULL will always be 0. ;)zero, write "0". IMHO, even in pointer context, "0" should be used - "NULL" provides no extra function except hiding what happens, and using if (pointer == 0) instead of if (pointer) is no improvement at all. And yes, it *is* guaranteed that NULL is 0, see: http://c-faq.com/null/index.html
Well, actually, that's not what it says. The hardware may choose to _not_ use 0 as a null pointer. The language, on the other hand, ensures that any compile-time 0's used in pointer contexts are translated to the appropriate hardware representation of a null pointer. So NULL is always defined as 0, but only because a constant 0 in a pointer context will always mean null pointer. This is very important if you were, say, writing an interrupt vector table and had the entries typed as ints rather than pointers. If you wrote 0 in that case, you are potentially defining a valid pointer.
That _only_ extends to compile-time though. That means that any form of runtime conversions from integers to pointers (a bad idea anyway, but it happens) can potentially cause problems if the hardware doesn't use 0 as a null pointer. Imagine an API that passes in data via int's. The implementation of the API knows that one of those int's is really a pointer. Since the compile-time version of the conversion is just a cast, it doesn't get any special treatment. If you were attempting to do sanity checking on the pointer value, you would need to be careful. If you test the int for 0, it will give the wrong answer on machines that use non-0 null pointers. If you test the pointer, the constant 0 used in the test will be converted to the appropriate null pointer and all is well.
I'd say that using NULL is actually clearer than 0 in this case. 0 is relying on intimate knowledge of the language and its special handling of 0 in pointer contexts. NULL, while always being defined as 0 or (void *)0, makes it clear that the end result is whatever the hardware needs for a null pointer.
Not that it actually makes any difference for 99.999% of us.
cu Michael _______________________________________________ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development
-- Rick Altherr kc8...@kc8apf.net"He said he hadn't had a byte in three days. I had a short, so I split it with him."
-- Unsigned
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ Openocd-development mailing list Openocd-development@lists.berlios.de https://lists.berlios.de/mailman/listinfo/openocd-development