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: PR 25512: pointer overflow defined?


On Wed, 21 Dec 2005, Paolo Carlini wrote:

> Gabriel Dos Reis wrote:
> 
> >| Humpf! Can people please cite exact paragraphs of the relevant
> >| Standards? Otherwise, I think we are just adding to the confusion. For
> >| example, in my reading of C99 6.5.9 and C++03 5.10 pointers *can* be
> >| compared for equality and discussing separately and correctly relational
> >| operators and equality operators is not a language-lawyer-ism, is *very*
> >| important for its real world implications. But this is only an example...
> >
> >I don't understand your query.
> >I understood Chris' comment as having to do with the implementation of
> >std::less<T*> (and friends) as required by C++.  Our implementation is just 
> >a forwarding function to operator< (and friends) on the assumption
> >that the compiler uses the "obvious" model.
> >  
> >
> Nobody disagree with that, of course (in fact, we discussed a bit that
> specific point with Chris time ago, when probably he wanted to avail
> himself of ordering to improve some bits of debug mode). Only, I become
> nervous when I read sentences like "pointers to different objects cannot
> be compared", without qualifications. Agreed, given sufficient context
> you can disambiguate, but I would appreciate a more precise way of
> casting the various points of views, supported by citations of the
> standard (e.g., like *you* are doing ;)

The point is, to do a comparison on pointers, you should do the math
on the appropriate (unsigned) integer type.  I.e. std::less for pointers
could look like

 template <class T>
 less(T*a, T*b) { return (uintptr_t)a < (uintptr_t)b; }

where you get the ordering defined by the bit-representation of the
pointer.  We document the implementation defined behavior here as

4.7 Arrays and pointers

    * The result of converting a pointer to an integer or vice versa (C90 
6.3.4, C99 6.3.2.3).

      A cast from pointer to integer discards most-significant bits if the 
pointer representation is larger than the integer type, sign-extends1 if 
the pointer representation is smaller than the integer type, otherwise the 
bits are unchanged.

i.e. exactly as you expect.

Richard.


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