This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: When is it legal to compare any pair of pointers?


----Original Message----
>From: Daniel Jacobowitz
>Sent: 14 September 2005 02:27

> On Tue, Sep 13, 2005 at 11:22:18AM +0100, chris jefferson wrote:
>> I realise that according to the C++ standard it isn't legal to compare
>> two pointers which are not from the same array. Is anyone aware of
>> anything in g++ which would actually forbid this, and if there is any
>> way of checking if will be valid?

> If two pointers to a T* are in the same array, their difference [in
> bytes] must be 0 modulo sizeof(T).  If they aren't, and sizeof(T) >
> alignof(T), then it doesn't have to be.  I'm pretty sure GCC can
> optimize based on this information.
> 
> This bit binutils, in the form of a crash in a hash function on
> Solaris.  I think that was pointer subtraction, rather than comparison,
> however.


  Wow!  :)  I nominate this for post of the year.

  I have archived it, and next time someone who _thinks_ they're being
clever by playing games behind the compiler's back tries to argue that
"pointers are all just numbers like ints" and "what could ever _actually_ go
wrong in practice", I'm going to pull it out, print it out on the side of a
concrete breeze block, and wack 'em upside the haid with it.

  Hey, you didn't even have to invoke ones-complement or segmented
architectures to get the nasal demons flying here.  Thanks for such an
invaluable piece of ammunition!


    cheers,
      DaveK
-- 
Can't think of a witty .sigline today....


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]