[Freeciv-Dev] Re: newbie C question
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Ben Webb wrote:
On Tue, 5 Mar 2002, Per I. Mathisen wrote:
NULL==0 is a valid assumption across all architectures and compilers?
Uh-oh. See http://www.eskimo.com/~scs/C-faq/s5.html.
My understanding is that
NULL == 0
is allways true, since this is the equivalent of
NULL == (void*)0
which is the equivalent of
NULL == NULL
I could be wrong on that first step, though. I *think* that
(int)NULL == (int)0
will _not_ always be true, since NULL is not always full of unset bits.
The examples I've seen have basically been of the form
int some_func(int x)
{
if (x == 0) printf("0!\n");
}
main()
{
some_func(NULL);
}
where x may or may not end up being 0, depending on platform. This just
seems like a more complex case of the int-casting equality check.
I'm quite positive that
int main()
{
char* p = NULL;
int x = 0;
if (memcmp(x, p, sizeof(int)) {
printf("1\n");
else
printf("2\n");
}
will give different results on different platforms (even disregarding
that sizeof(char*) may be different on different platforms). I am
equally positive that
assert(!NULL);
will pass on all platforms. Somewhere in between, the comparison fails.
That said, I've never seen or even heard of someone having problems with
this. But I do try to mostly initialize NULL as NULL rather than just
use memset.
jason
[Freeciv-Dev] Re: newbie C question, Raimar Falke, 2002/03/05
|
|