Nonportable code in C++ support
Esben Mose Hansen
kde at mosehansen.dk
Wed Feb 13 18:38:57 UTC 2008
On Wednesday 13 February 2008 17:13:16 Kuba Ober wrote:
> 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
You mean, in the case you get many hash collisions across 4Gb of memory?
Personally, I'd go with the code from the c++ standard library:
except that the cast would be to int :) Simple, fast, and very likely to
return different values for different pointers, even modulo some number
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
More information about the KDevelop-devel