monotone-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Monotone-devel] pointer arithmetics in C++


From: Zack Weinberg
Subject: Re: [Monotone-devel] pointer arithmetics in C++
Date: Fri, 15 Sep 2006 18:12:32 -0700

On 9/15/06, Nathaniel Smith <address@hidden> wrote:
On Fri, Sep 15, 2006 at 10:17:05PM +0200, Markus Schiltknecht wrote:
> I know, I know, it's evil. I want to do some pointer arithmetics
> anyway... I need to build a hash or digest or something from three
> 'unsigned int' values and a pointer.

Is it really the pointer that you need to hash, not the value it
points to?  Could you give more details about what exactly you're
trying to do? :-)

It should be mentioned that pointer hashing makes your program
nondeterministic, dependent on details of memory layout - you may well
see bugs that depend on things like how much memory is required to
store all the environment variables.  As such it is almost always a
bad idea...

  x1 ^ x2 ^ x3 ^ reinterpret_cast<int>(my_pointer);

(Or perhaps multiplication would be better, or something, hash
functions are not that easy to design.)

Note also that pointers tend to have lots of zero bits in predictable
places; I don;t know whether that makes XOR good or bad, but it's
something to think about.

Except that this assumes that pointers and integers are the same width
on your machine, i.e., it's not 64-bit safe, which is Bad.

size_t is the size of a pointer in all sane ABIs (i.e. everything
except weird embedded chips and 64-bit Windows); if you have C99's
inttypes.h and/or stdint.h, intptr_t there is guaranteed to be the
size of a pointer, although it may not exist.

zw




reply via email to

[Prev in Thread] Current Thread [Next in Thread]