From owner-freebsd-arch Tue Aug 20 18:45:49 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 7EBAF37B400 for ; Tue, 20 Aug 2002 18:45:44 -0700 (PDT) Received: from mailg.telia.com (mailg.telia.com [194.22.194.26]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9846043E70 for ; Tue, 20 Aug 2002 18:45:42 -0700 (PDT) (envelope-from erikt@midgard.homeip.net) Received: from d1o913.telia.com (d1o913.telia.com [195.252.44.241]) by mailg.telia.com (8.12.5/8.12.5) with ESMTP id g7L1jeGU015969 for ; Wed, 21 Aug 2002 03:45:40 +0200 (CEST) X-Original-Recipient: Received: from falcon.midgard.homeip.net (h62n2fls20o913.telia.com [212.181.163.62]) by d1o913.telia.com (8.8.8/8.8.8) with SMTP id DAA06238 for ; Wed, 21 Aug 2002 03:45:39 +0200 (CEST) Received: (qmail 5587 invoked by uid 1001); 21 Aug 2002 01:45:37 -0000 Date: Wed, 21 Aug 2002 03:45:37 +0200 From: Erik Trulsson To: Jon Mini Cc: Archie Cobbs , obrien@FreeBSD.ORG, freebsd-arch@FreeBSD.ORG Subject: Re: NULL Message-ID: <20020821014537.GA5553@falcon.midgard.homeip.net> Mail-Followup-To: Jon Mini , Archie Cobbs , obrien@FreeBSD.ORG, freebsd-arch@FreeBSD.ORG References: <20020821002116.GA33223@dragon.nuxi.com> <200208210101.g7L110m03801@arch20m.dellroad.org> <20020821012849.GK3751@elvis.mu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020821012849.GK3751@elvis.mu.org> User-Agent: Mutt/1.5.1i 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, Aug 20, 2002 at 06:28:49PM -0700, 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. > > "Not legal" refers to the fact that C is a standardised language, > and this violates that standard. Whether or not it works in gcc is > irrellevant. Correction: This violates the C++ standard, not the C standard. In C NULL can be defined as either 0 or ((void*)0). In C++ only 0 is allowed. The reason for this is that C++ has stricter typechecking such that char *p = ((void*)0) is not allowed since one would assign a void pointer to a char pointer. This is allowed in C but not C++. In C you can always assign a void pointer to any other sort of pointer and it will automatically be converted to the corrct type. Not so in C++. > > Also, NULL is defined as 0 in the standard, because this: > > void *p; > > p = 0; > > Is guaranteed to produce an invalid pointer, and this: Not really invalid pointer, rather a pointer which is guaranteed not to point to any object. (And which will compare equal to any other null pointer and not equal to any valid non-null pointer.) > > ((p != 0) || (p == 0)) > > Tests for a valid pointer and an invalid pointer, respectively. Actually it tests for non-null pointer and null pointer respectively. Not all non-null pointers are valid. > > 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." True but irrelevant. > > On a related note, this : > > p = 1; > > Is illegal. Not illegal. It just causes undefined (or possibly implementation defined) behaviour. The compiler may (and usually will) accept. This is in contrast to C++ where char * p= ((void*0)) will cause a compilation error. > > I hope this makes sense. > > -- > Jonathan Mini > http://www.freebsd.org/ -- Erik Trulsson ertr1013@student.uu.se To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-arch" in the body of the message