Given an object obj is there a guarantee that
uintptr(unsafe.Pointer(&obj))
will always evaluate to the same value regardless of w
There isn't anything in the specification that guarantees this, probably to allow implementations of the language to use compacting garbage collectors in the future. In this golang-nuts thread one of the developers suggests that a compacting GC would be possible provided unsafe.Pointer
values were pinned in memory, but this couldn't extend to all unitptr
values.
For the current Go runtime I believe it is true, but relying on it would still be undefined behaviour. There are a few caveats though:
If obj
is a zero size type, the value of the expression may not be unique, as described in the spec.
Over the lifetime of a program, a particular uintptr
value might refer to different objects.