From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17936 invoked by alias); 12 Jul 2005 21:42:30 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 17927 invoked by uid 22791); 12 Jul 2005 21:42:26 -0000 Received: from pne-smtpout1-sn1.fre.skanova.net (HELO pne-smtpout1-sn1.fre.skanova.net) (81.228.11.98) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Tue, 12 Jul 2005 21:42:26 +0000 Received: from falcon.midgard.homeip.net (212.181.162.201) by pne-smtpout1-sn1.fre.skanova.net (7.2.060.1) id 42B813B0003BB23B for gcc@gcc.gnu.org; Tue, 12 Jul 2005 23:42:24 +0200 Received: (qmail 90162 invoked by uid 1001); 12 Jul 2005 23:42:23 +0200 Date: Tue, 12 Jul 2005 21:42:00 -0000 From: Erik Trulsson To: Dave Korn Cc: 'Daniel Berlin' , mrc.lrn@inwind.it, gcc@gcc.gnu.org Subject: Re: Pointers in comparison expressions Message-ID: <20050712214222.GA90111@falcon.midgard.homeip.net> Mail-Followup-To: Dave Korn , 'Daniel Berlin' , mrc.lrn@inwind.it, gcc@gcc.gnu.org References: <1121185961.13154.50.camel@linux-009002219143> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.9i X-SW-Source: 2005-07/txt/msg00527.txt.bz2 On Tue, Jul 12, 2005 at 05:54:00PM +0100, Dave Korn wrote: > ----Original Message---- > >From: Daniel Berlin > >Sent: 12 July 2005 17:33 > > >> I think that even if the use of relational operators other than '==' and > >> '!=' is legal with pointers, the compiler should issue a warning (when > >> the option -Wall is used), as it does for assignment, used as truth > >> values, not surrounded with parentheses. > > > > Why? > > It's legal, it's useful, and used. > > > > --Dan > > > Just to enlarge upon Dan's comment: > > Since pointer subtraction is well defined, and it returns an int, then ... > > int *a, *b; > > if (a < b) > dosomething (); > > ... is just the same as ... > > int *a, *b; > > if ((b - a) >= 0) > dosomething (); > > ... so do you think the compiler should warn about _all_ pointer arithmetic? Pointer subtraction is only well defined if both pointers point to elements in the same array (or one past the end of the array). Otherwise the behaviour is undefined. Relational operators between pointers are only defined if both pointers either point to elements in the same array (or one past the end of the array), or to members of the same structure. Otherwise the result is undefined. Thus, if you have code like: int aa,bb; int *a, *b; a=&aa; b=&bb; Then expressions like "(a>b)" or "(a-b)" both have undefined behaviour, while if you had code like int aa[2]; int *a, *b; a=&(aa[0]); b=&(aa[1]); Then the expressions "(a>b)" and "(a-b") are both well defined (and will have the values 0 and 1 respectively.) If the compiler is certain that the pointers do not point into the same array or structure (as in my first example above) it is probably a good idea to give a warning, but it should not warn for the legal cases (as in my second example.) -- Erik Trulsson ertr1013@student.uu.se