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: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: newbie C question
From: Jason Short <vze2zq63@xxxxxxxxxxx>
Date: Tue, 05 Mar 2002 22:35:08 -0500
Reply-to: jdorje@xxxxxxxxxxxx

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



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