doucheyi1347
doucheyi1347
2017-08-22 13:26

如何在Go中比较指针?

已采纳

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条回答

  • doushi2845 doushi2845 4年前

    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.

    点赞 评论 复制链接分享

相关推荐