如何存储指针所指向的地址?

huangapple go评论142阅读模式
英文:

How to store the address pointed to by a pointer?

问题

我正在尝试创建一个地址映射表,用于存储我使用new()创建的对象的地址和分配时间。键是通过调用new()返回的地址。如何获取new()返回的地址?

type T struct{a, b int }

func main(){
        
        var t int64 = time.Nanoseconds()
        memmap := make(map[uintptr]int64)
        fmt.Printf("%d\n", t)
        var ptr *T = new(T)
        ptr.a = 1
        ptr.b = 2
        fmt.Printf("%d %d %p %T\n", ptr.a, ptr.b, ptr, ptr)
        memmap[uintptr(unsafe.Pointer(ptr))] = t
}

请告诉我在映射表中键字段的类型应该是什么,以便我可以存储new()返回的地址?我计划使用不同类型的new(),获取分配的地址并将其与创建时间进行映射。

英文:

I am trying to create a map of addresses of objects that I create with the time at which it is allocated. The key is the address returned by the call to new(). How do I get the address returned by new()?

type T struct{a, b int }

func main(){
        
        var t int64 = time.Nanoseconds()
        memmap := make(map[uint8]int64)
        fmt.Printf("%d\n", t)
        var ptr *T = new(T)
        ptr.a = 1
        ptr.b = 2
        fmt.Printf("%d %d %p %T\n", ptr.a, ptr.b, ptr, ptr)
        //memmap[ptr] = t //gives error
        //var temp uint8 = ptr//gives error
}

Please tell me what should be the type of the key field in the map so that I can store the address returned by new()? I plan to use new() with different types, get the allocated address and map it with the creation time.

答案1

得分: 7

你可以使用unsafe包中的Pointer类型,但是正如包名所暗示的那样,它是不安全的。地址本身是一个不透明的东西,实际上仅使用地址值本身来进行映射是很少有用的,最好使用类型和地址的元组。这就是unsafe.Reflect提供的功能。reflect包提供了UnsafeAddr函数和更多功能。

我建议你阅读reflectunsafe包的文档。

英文:

You can use the type Pointer from the unsafe package, but that is, the package name implies it, unsafe. The address itself is a opaque thing and there's only little use in actually using a address value alone for a map, better use a tuple of type and address. That's what unsafe.Reflect does provide you. The package reflect offers you the function UnsafeAddr and a lot more.

I suggest you read the package documentation for reflect and unsafe packages.

答案2

得分: 3

使用uintptr,一个足够大的无符号整数来存储指针值的未解释位,作为内存映射的键类型。

例如,

package main

import (
	"fmt"
	"time"
	"unsafe"
)

type T struct{ a, b int }

func main() {
	memmap := make(map[uintptr]int64)

	pT := new(T)
	memmap[uintptr(unsafe.Pointer(pT))] = time.Nanoseconds()
	pT.a = 1
	pT.b = 2
	fmt.Printf("%d %d %p %T\n", pT.a, pT.b, pT, pT)

	pI := new(int)
	memmap[uintptr(unsafe.Pointer(pI))] = time.Nanoseconds()
	*pI = 42
	fmt.Printf("%d %p %T\n", *pI, pI, pI)

	fmt.Printf("\n%T\n", memmap)
	for k, v := range memmap {
		fmt.Printf("%x: %d\n", k, v)
	}
}

输出:

1 2 0xf8400001f8 *main.T
42 0xf8400001f0 *int

map[uintptr] int64
f8400001f0: 1306837191421330000
f8400001f8: 1306837191421293000
英文:

Use uintptr, an unsigned integer large enough to store the uninterpreted bits of a pointer value, as the memory map key type.

For example,

package main

import (
	"fmt"
	"time"
	"unsafe"
)

type T struct{ a, b int }

func main() {
	memmap := make(map[uintptr]int64)

	pT := new(T)
	memmap[uintptr(unsafe.Pointer(pT))] = time.Nanoseconds()
	pT.a = 1
	pT.b = 2
	fmt.Printf("%d %d %p %T\n", pT.a, pT.b, pT, pT)

	pI := new(int)
	memmap[uintptr(unsafe.Pointer(pI))] = time.Nanoseconds()
	*pI = 42
	fmt.Printf("%d %p %T\n", *pI, pI, pI)

	fmt.Printf("\n%T\n", memmap)
	for k, v := range memmap {
		fmt.Printf("%x: %d\n", k, v)
	}
}

Output:

1 2 0xf8400001f8 *main.T
42 0xf8400001f0 *int

map[uintptr] int64
f8400001f0: 1306837191421330000
f8400001f8: 1306837191421293000

huangapple
  • 本文由 发表于 2011年5月31日 14:24:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/6183739.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定