How can I compare pointers in Go?

故事扮演 提交于 2019-12-14 03:33:18

问题


I am building some test code and I try to compare pointers with the <, > operators. My goal is to order elements into a balanced tree structure (implemented by me). For that, I need quick, generic comparation. Following the good old C/C++ customs, my current idea is (...was, at the time of writing this post) to compare them by their pointer addresses.

However, as I've found, it doesn't work. The source

if (&(a.val) < &(b.val)) {

gives the compile error

./pointer.go:40: invalid operation: &a.val < &b.val (operator < not defined on pointer)

a and b are structs with val as (safe) pointer member.

Is it possible to compare safe pointers in go? How?

Is it guaranteed that the order of the pointers remains the same? (For example, I can imagine some GC trickery which also reorders the data.)


回答1:


Is it guaranteed that the order of the pointers remains the same? (For example, I can imagine some GC trickery which also reorders the data.)

In answer to this part of your question, it doesn't look like it. See pkg unsafe linked below.

If you want the memory address of a pointer, try the unsafe package:

https://golang.org/pkg/unsafe/#Pointer

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    var a,b int
    var pa,pb *int
    pa = &a
    pb = &b

    //var c int
    //pa = &c

    if uintptr(unsafe.Pointer(pa)) < uintptr(unsafe.Pointer(pb)) {
      fmt.Printf("a:%p > b:%p",pa,pb)   
    } else {
      fmt.Printf("b:%p > a:%p",pb,pa)
    }

}

This will let you get an arbitrary Pointer type and then the current memory address of that pointer (as printf would). Note the caveats there though, you cannot rely on this address:

Converting a Pointer to a uintptr produces the memory address of the value pointed at, as an integer. The usual use for such a uintptr is to print it. Conversion of a uintptr back to Pointer is not valid in general. A uintptr is an integer, not a reference. Converting a Pointer to a uintptr creates an integer value with no pointer semantics. Even if a uintptr holds the address of some object, the garbage collector will not update that uintptr's value if the object moves, nor will that uintptr keep the object from being reclaimed.

This bypasses the Go type system and memory security so it's unsafe and you probably don't want to do it, unless you're just experimenting. I can't think of a reason to do it in tests, but if you want to be sure to read that entire document and be very sure you know that what you are doing will work as you expect.



来源:https://stackoverflow.com/questions/45818964/how-can-i-compare-pointers-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!