From owner-freebsd-arch Tue Aug 20 19: 5:25 2002 Delivered-To: freebsd-arch@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 72F4D37B400; Tue, 20 Aug 2002 19:05:21 -0700 (PDT) Received: from mailman.zeta.org.au (mailman.zeta.org.au [203.26.10.16]) by mx1.FreeBSD.org (Postfix) with ESMTP id 111DD43E3B; Tue, 20 Aug 2002 19:05:20 -0700 (PDT) (envelope-from bde@zeta.org.au) Received: from bde.zeta.org.au (bde.zeta.org.au [203.2.228.102]) by mailman.zeta.org.au (8.9.3/8.8.7) with ESMTP id CAA24501; Wed, 21 Aug 2002 02:05:16 GMT Date: Wed, 21 Aug 2002 12:10:47 +1000 (EST) From: Bruce Evans X-X-Sender: bde@gamplex.bde.org To: Jon Mini Cc: Archie Cobbs , , Subject: Re: NULL In-Reply-To: <20020821012849.GK3751@elvis.mu.org> Message-ID: <20020821114425.Q26007-100000@gamplex.bde.org> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Tue, 20 Aug 2002, Jon Mini wrote: > Archie Cobbs [archie@dellroad.org] wrote : > > > When you say "not legal" do you mean it causes an error or a warning? > > > > FYI, this question came up when porting some code to redhat Linux, where > > NULL is defined as (void *)0. This definition seems to be missing some parentheses. In C, 0 and ((void *)0) are just 2 of many different correct definitions of NULL. They cause different warnings and error in broken code, so it may be useful to try compiling with both in an attempt to detect more errors. > "Not legal" refers to the fact that C is a standardised language, ^ I think you mean C++ > and this violates that standard. Whether or not it works in gcc is > irrellevant. > > Also, NULL is defined as 0 in the standard, because this: > > void *p; > > p = 0; > > Is guaranteed to produce an invalid pointer, and this: > > ((p != 0) || (p == 0)) > > Tests for a valid pointer and an invalid pointer, respectively. > > The fact that pointers are linear addresses in FreeBSD and Linux > and that the address value 0x0 is used for NULL are just some of > the happy coincidences that the relevant standards can't rely on, > and must define as "implementation dependant." Address 0 has very little to do with NULL, at least in C. In C, NULL expands to an implementation-defined null pointer constant. A null pointer constant is an integer constant expression [of any integer type] with value 0, or such an expression cast to precisely (void *). C compilers can easily recognize these expressions and, if they are in pointer contexts (even despite not being pointers themselves), translate them to implementation-decided magic address values which might not be all-bits-0. > On a related note, this : > > p = 1; > > Is illegal. > > I hope this makes sense. This is invalid in C, too, basically because the integral constant expression 1 doesn't have value 0, so the magic rules for 0 don't apply. More general but stricter rules about conversions between pointers and integers apply instead. Bruce To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-arch" in the body of the message