Does Go guarantee constant addresses?

后端 未结 3 536
挽巷
挽巷 2021-02-07 13:22

Given an object obj is there a guarantee that

uintptr(unsafe.Pointer(&obj))

will always evaluate to the same value regardless of w

3条回答
  •  礼貌的吻别
    2021-02-07 14:27

    No absolute guarantee. Especially if Go adds compaction to its mark and sweep garbage collector.

    Addresses stored in pointer types and type unsafe.Pointer will be updated, if necessary, by any garbage collector. Addresses stored in type uintptr as unsigned integers will not be updated by a garbage collector. The uintptr type is not a pointer type, it's an integer type.

    Numeric types

    uintptr an unsigned integer large enough to store the uninterpreted bits of a pointer value

    converting unsafe.Pointers to uintptr

    Pointers should have been kept in unsafe.Pointers - not uintptrs - always.

    Russ

    For your example,

    uintptr(unsafe.Pointer(&obj))
    

    you have an unsigned integer, not an address.

提交回复
热议问题