Nonportable code in C++ support

Kuba Ober kuba at mareimbrium.org
Wed Feb 13 16:13:16 UTC 2008


On Wednesday 13 February 2008, Kris Wong wrote:
> > But since the hash-values are always 32 bit, won"t you then
> > get a warning when
> > casting long -> uint? :) Of course, any way of removing the
> > warning is fine.
>
> int hash = (int)((size_t)ptr + [whatever]);
>
> Let's not forget, longs are 4 bytes on 64 bit Windows.

Wouldn't this work better? It should be portable, too, although
it has a benign warning on 32 bits.

void * ptr;
unsigned long lptr = (unsigned long)ptr;
lptr = (lptr >> 32) ^ (lptr & 0xffffffff);
int hash = (int)lptr;

The hash value on 32 bit platforms should be simply the value of the
pointer, while on 64 bits it will be the upper and lower halves xor-ed
together.

Cheers, Kuba




More information about the KDevelop-devel mailing list