From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5658 invoked by alias); 12 Jul 2005 22:09:02 -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 5618 invoked by uid 22791); 12 Jul 2005 22:08:57 -0000 Received: from us01smtp1.synopsys.com (HELO boden.synopsys.com) (198.182.44.79) by sourceware.org (qpsmtpd/0.30-dev) with ESMTP; Tue, 12 Jul 2005 22:08:57 +0000 Received: from maiden.synopsys.com (maiden.synopsys.com [146.225.100.170]) by boden.synopsys.com (Postfix) with ESMTP id 29ED2DAE0; Tue, 12 Jul 2005 15:07:55 -0700 (PDT) Received: from piper.synopsys.com (localhost [127.0.0.1]) by maiden.synopsys.com (8.9.1/8.9.1) with ESMTP id PAA14667; Tue, 12 Jul 2005 15:08:54 -0700 (PDT) Received: from piper.synopsys.com (localhost [127.0.0.1]) by piper.synopsys.com (8.12.10/8.12.3) with ESMTP id j6CM8sOe007081; Tue, 12 Jul 2005 15:08:54 -0700 Received: (from jbuck@localhost) by piper.synopsys.com (8.12.10/8.12.10/Submit) id j6CM8scn007079; Tue, 12 Jul 2005 15:08:54 -0700 Date: Tue, 12 Jul 2005 22:09:00 -0000 From: Joe Buck To: Dave Korn , "'Daniel Berlin'" , mrc.lrn@inwind.it, gcc@gcc.gnu.org Subject: Re: Pointers in comparison expressions Message-ID: <20050712220854.GA7068@synopsys.com> References: <1121185961.13154.50.camel@linux-009002219143> <20050712214222.GA90111@falcon.midgard.homeip.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050712214222.GA90111@falcon.midgard.homeip.net> User-Agent: Mutt/1.4.1i X-SW-Source: 2005-07/txt/msg00528.txt.bz2 On Tue, Jul 12, 2005 at 11:42:23PM +0200, Erik Trulsson wrote: > 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. While this is correct, there are certain cases that the standard leaves undefined but that nevertheless can be useful; for example, pointer subtraction can be used to estimate the amount of stack in use (of course, it is necessary to know if the stack grows upward or downward). Similarly, in an OS kernel, comparing pointers may make sense. > 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.) I'd say that would be only useful in terms of a more general analysis that implements bounds checking. Given reliable analysis that finds suspicious pointer comparisons, most users would be more interested in the implicit comparison of a pointer with the boundaries of the array it corresponds to.