A pointer references a location in memory. Actually all memory addresses have the same type independently of the variable type as far as I understand.
指针引用内存中的位置。实际上,据我所知,所有内存地址都具有独立于变量类型的相同类型。
Instead of using different pointer type (*int
, *string
etc..), is this possible using only one type (var p pointer
) for all the pointer types?
而不是使用不同的指针类型(* int,* string etc ..),这可能只使用一种类型(var p指针)用于所有指针类型?
What is the difference between different pointer types?
不同指针类型之间有什么区别?
package main
import "fmt"
func main() {
i := 5
s := "abc"
var pi *int // alternatively var pi pointer
var ps *string // alternatively var ps pointer
pi = &i
ps = &s
fmt.Printf("%p %p", pi, ps) // result is 0x1040e0f8 0x1040a120
}
2 个解决方案
#1
1
is this possible using only one type for all the pointer types?
所有指针类型只能使用一种类型吗?
Yes, that is pretty much how C works. Unfortunately, that makes the language dangerous. Say you have an array of 10 bytes. If you JUST pass the pointer, the other code won't know how many bytes it's safe to access. That leads to all kinds of buffer overflow errors. (i.e. HeartBleed)
是的,这几乎就是C的工作原理。不幸的是,这使语言变得危险。假设你有一个10字节的数组。如果你只是传递指针,其他代码将不知道可以安全访问多少字节。这会导致各种缓冲区溢出错误。 (即HeartBleed)
In Go, they pointer knows the type of the thing it points to, so it can prevent your code from having buffer overflow problems all the time.
在Go中,它们指针知道它指向的东西的类型,因此它可以防止代码一直出现缓冲区溢出问题。
You can do what you want, but only by using the Unsafe package. As the name suggests, that is a very dangerous thing to do.
你可以做你想要的,但只能使用不安全的包。顾名思义,这是一件非常危险的事情。
Perhaps if you post what you are ACTUALLY trying to do, people can help you. Using unsafe pointers is not the only way to write performant code.
也许如果你发布你实际上想要做的事情,人们可以帮助你。使用不安全的指针并不是编写高性能代码的唯一方法。
#2
2
The type system in Go is designed to prevent memory errors relating to pointers. This allows programmers to have enough control to manipulate objects in memory while Allowing the garbage collector top cop moody of the heavy lifting.
If you need to manually memory and convert pointer types you can use the unsafe package.
Go中的类型系统旨在防止与指针相关的内存错误。这允许程序员有足够的控制来操纵内存中的对象,同时允许垃圾收集器顶部警察喜怒无常。如果需要手动存储和转换指针类型,可以使用不安全的包。
#1
1
is this possible using only one type for all the pointer types?
所有指针类型只能使用一种类型吗?
Yes, that is pretty much how C works. Unfortunately, that makes the language dangerous. Say you have an array of 10 bytes. If you JUST pass the pointer, the other code won't know how many bytes it's safe to access. That leads to all kinds of buffer overflow errors. (i.e. HeartBleed)
是的,这几乎就是C的工作原理。不幸的是,这使语言变得危险。假设你有一个10字节的数组。如果你只是传递指针,其他代码将不知道可以安全访问多少字节。这会导致各种缓冲区溢出错误。 (即HeartBleed)
In Go, they pointer knows the type of the thing it points to, so it can prevent your code from having buffer overflow problems all the time.
在Go中,它们指针知道它指向的东西的类型,因此它可以防止代码一直出现缓冲区溢出问题。
You can do what you want, but only by using the Unsafe package. As the name suggests, that is a very dangerous thing to do.
你可以做你想要的,但只能使用不安全的包。顾名思义,这是一件非常危险的事情。
Perhaps if you post what you are ACTUALLY trying to do, people can help you. Using unsafe pointers is not the only way to write performant code.
也许如果你发布你实际上想要做的事情,人们可以帮助你。使用不安全的指针并不是编写高性能代码的唯一方法。
#2
2
The type system in Go is designed to prevent memory errors relating to pointers. This allows programmers to have enough control to manipulate objects in memory while Allowing the garbage collector top cop moody of the heavy lifting.
If you need to manually memory and convert pointer types you can use the unsafe package.
Go中的类型系统旨在防止与指针相关的内存错误。这允许程序员有足够的控制来操纵内存中的对象,同时允许垃圾收集器顶部警察喜怒无常。如果需要手动存储和转换指针类型,可以使用不安全的包。