Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2002:
[Freeciv-Dev] Re: newbie C question
Home

[Freeciv-Dev] Re: newbie C question

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: jdorje@xxxxxxxxxxxxxxxxxxxxx
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: newbie C question
From: Ben Webb <ben@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 6 Mar 2002 10:00:28 +0000 (GMT)

On Tue, 5 Mar 2002, Jason Short wrote:

> 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

        This is why I said "uh-oh". ;) This is also my understanding, 
since NULL is usually just #defined as 0 (or (void *)0). But (and it's a 
big but) the null pointer (not NULL) is not necessarily compromised of all 
zero bits - i.e. the C compiler automatically converts a 0 (or NULL) into 
the null pointer. This is why you shouldn't use memset or calloc to 
initialise a pointer - always use an explicit pt = NULL assignment (and I 
speak as a past - and probably present - offender).

>    (int)NULL == (int)0
> 
> will _not_ always be true, since NULL is not always full of unset bits. 

        No, NULL is zero. It's only once you assign its value to a pointer 
that the _pointer_ takes on the magic null pointer properties, which may 
not be always full of unset bits. At least, that's my understanding. So

(int)NULL == (int)0;  will be true, but

int a = 0;
char *p = NULL;
a == p;               will not always be.

> 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.

        The URL above leads on to a list of platforms on which it can be 
a problem... but I've never heard of most of them. ;)

        Ben
-- 
ben@xxxxxxxxxxxxxxxxxxxxxx           http://bellatrix.pcl.ox.ac.uk/~ben/
"Heavier-than-air flying machines are impossible."
         - Lord Kelvin, president, Royal Society, 1895.



[Prev in Thread] Current Thread [Next in Thread]