我如何比较Go中的指针? [英] How can I compare pointers in Go?

查看:326
本文介绍了我如何比较Go中的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一些测试代码,并试图将指针与< > 运算符进行比较。我的目标是将元素排列成一个平衡的树结构(由我实现)。为此,我需要快速,通用的比较。遵循良好的旧C / C ++习俗,我目前的想法是(在写这篇文章的时候)通过指针地址比较它们。



然而,正如我发现的那样,它不起作用。来源

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

给出编译错误


./ pointer.go:40:无效操作:& a.val< & b.val(运算符<未在指针上定义)


a b 的结构是 val 作为(安全)指针成员。



是否有可能比较安全指针?如何?



是否保证指针的顺序保持不变? (例如,我可以想象一些GC技巧,它也会重新排序数据)。

解决方案


它保证指针的顺序保持不变? (对于
例子,我可以想象一些GC技巧,它也会重新排序数据)。

在回答这部分你的问题,它看起来不像。请参阅下面的pkg不安全链接。



如果你想要一个指针的内存地址,请试试这个不安全的包:

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

 包主要

导入(
fmt
不安全


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)
}

}

这会让你得到一个任意的指针类型,然后得到该指针的当前内存地址(如printf所示)。注意这里的注意事项,你不能依赖这个地址:

lockquote

将指针转换为uintptr会产生$ b $的内存地址b值指向,作为一个整数。这种uintptr的通常用法是
来打印它。将uintptr转换回指针在
general中无效。 uintptr是一个整数,而不是引用。将
指针转换为uintptr将创建一个不带指针
语义的整数值。即使uintptr持有某个对象的地址,如果对象
移动,
垃圾回收器也不会更新该uintptr的值,也不会使该对象不被回收。


绕过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.)

解决方案

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.

这篇关于我如何比较Go中的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆