How do I declare a pointer at a specific memory address in go golang and store a value there

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

How do I declare a pointer at a specific memory address in go golang and store a value there

问题

所以假设我绝对需要在Go语言中将一个值存储在特定的内存地址0xc0000140f0,我该如何做呢?例如:

package main

import (
	"fmt"
	"unsafe"
)

func main() {
	targetAddress := 0xc0000140f0
	loc := (uintptr)(unsafe.Pointer(targetAddress))
	p := unsafe.Pointer(loc)
	var val int = *((*int)(p))
	fmt.Println("Location : ", loc, " Val :", val)
}

这会导致以下错误:

./memory.go:10:33: 无法将targetAddress(类型为int)转换为类型unsafe.Pointer
英文:

So let's say that I absolutely NEED to store a value at a specific memory address of 0xc0000140f0 in go. How would I do this. For example:

package main

import (
	"fmt"
	"unsafe"
)

func main() {
	targetAddress := 0xc0000140f0
	loc := (uintptr)(unsafe.Pointer(targetAddress))
	p := unsafe.Pointer(loc)
	var val int = *((*int)(p))
	fmt.Println("Location : ", loc, " Val :", val)
}

This results in the following errors:

./memory.go:10:33: cannot convert targetAddress (type int) to type unsafe.Pointer

答案1

得分: 5

根据错误提示,你的类型转换是无效的。根据 unsafe.Pointer文档

  • 任何类型的指针值都可以转换为 Pointer
  • Pointer 可以转换为任何类型的指针值。
  • uintptr 可以转换为 Pointer
  • Pointer 可以转换为 uintptr

请注意,大写的 "Pointer" 是指 unsafe.Pointer,而 "pointer value" 是指常规的 Go 指针,如 *int

Go 有一个严格的类型系统,所以你需要检查你使用的适当类型,并注意类型错误。

你的代码的正确版本,尝试从给定的内存地址加载值,如下所示:

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    loc := uintptr(0xc0000140f0)
    p := unsafe.Pointer(loc)
    var val int = *((*int)(p))
    fmt.Println("Location : ", loc, " Val :", val)
}

正如标题所示,你还想要存储一个值,代码如下:

*((*int)(p)) = 1234

现在,如果你想要保持该指针以继续使用它,你可以将其存储为常规的 Go 指针:

var pointer *int = (*int)(p)
val := *pointer // 加载某个值
*pointer = 456 // 存储某个值

当然,这里使用的 int 类型完全是任意的。你可以使用任何类型,这将决定在这个上下文中 "a value" 的含义。

英文:

As the error says, your type conversion is invalid. From the unsafe.Pointer documentation:
>- A pointer value of any type can be converted to a Pointer.
>- A Pointer can be converted to a pointer value of any type.
>- A uintptr can be converted to a Pointer.
>- A Pointer can be converted to a uintptr.

Note that "Pointer" (caps) refers to unsafe.Pointer, while "pointer value" refers to regular Go pointers like *int

Go has a strict type system, so you need to check the appropriate types for what you're using, and pay attention to type errors.

The correct version of your code, which tries to load the value from the given memory address, is this:

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    loc := uintptr(0xc0000140f0)
    p := unsafe.Pointer(loc)
    var val int = *((*int)(p))
    fmt.Println("Location : ", loc, " Val :", val)
}

As the title suggests, you also want to store a value, which would look like this:

*((*int)(p)) = 1234

Now if you want to maintain that pointer to keep using it, you can store it as a regular Go pointer:

var pointer *int = (*int)(p)
val := *pointer // load something
*pointer = 456 // store something

Of course, the use of int here is completely arbitrary. You could use any type, which will determine the meaning of "a value" in this context.

huangapple
  • 本文由 发表于 2021年7月10日 02:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/68321230.html
匿名

发表评论

匿名网友

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

确定