When is it legal to compare any pair of pointers?

Joe Buck Joe.Buck@synopsys.COM
Wed Sep 14 18:29:00 GMT 2005


On Wed, Sep 14, 2005 at 02:15:43PM -0300, Alexandre Oliva wrote:
> On Sep 13, 2005, Daniel Jacobowitz <drow@false.org> wrote:
> 
> > 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.
> 
> > Perhaps someone who remembers this problem more clearly than
> > I do can chip in if I've gotten it totally wrong.
> 
> Yep, it was pointer subtraction, and GCC actually optimized the
> division, that could in theory be assumed to be exact, into a
> multiplication by a large constant (aah, the wonders of modulo
> arithmetics :-), and that's what broke some sorting function on
> Solaris.  And I was the lucky guy who got to debug that :-)

People that don't like the GCC optimization should be prepared to take
a very large speed penalty on inner loops that have a pointer subtraction
for an array or std::vector of objects whose size is not a power of two.
There are processors where integer division is 20x slower than
integer multiplication, or even more.



More information about the Gcc mailing list