英文:
Golang memory allocation for string copy memory addresses
问题
我目前正在阅读《Go编程语言》这本书,书中描述了字符串或子字符串的副本具有相似的内存地址。
s := "hello"
c := s
fmt.Println(&s, &c) // 输出 0xc000010230 0xc000010240
我的问题是,既然c
是一个完全的副本,那么&c
不应该与&s
相同吗?
RAM
地址 | 值
&s 0xc000010230 | "hello" <----- s
&c 0xc000010240 | "hello" <----- c
英文:
I am currently reading the Go Programming Language book which describes that a copy of a string or a substring has similar memory addresses.
s := "hello"
c := s
fmt.Println(&s, &c) // prints 0xc000010230 0xc000010240
My question is, shouldn't &c
be the same as &s
since it c
is an exact copy ?
RAM
Address | Value
&s 0xc000010230 | "hello" <----- s
&c 0xc000010240 | "hello" <----- c
</details>
# 答案1
**得分**: 5
`c`和`s`实际上是两个不同的字符串头部。但它们都指向同一个`"hello"`。
```go
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
ch := (*reflect.StringHeader)(unsafe.Pointer(&c))
fmt.Println(sh.Data, ch.Data)
https://go.dev/play/p/Ckl0P3g4nVo
字符串头部的Data
字段指向字符串中的第一个字节,字符串头部的Len
字段表示字符串的长度。你可以使用这些信息来确认字符串头部是否指向原始字符串。
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
for i := 0; i < sh.Len; i++ {
sp := (*byte)(unsafe.Pointer(sh.Data + uintptr(i)))
fmt.Printf("%p = %c\n", sp, *sp)
}
https://go.dev/play/p/LFfdxxARw1f
英文:
c
and s
are actually two distinct string headers. But both of them point to the same "hello"
.
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
ch := (*reflect.StringHeader)(unsafe.Pointer(&c))
fmt.Println(sh.Data, ch.Data)
https://go.dev/play/p/Ckl0P3g4nVo
The Data
field of the string header points to the first byte
in the string and the Len
field of the string header indicates the length of the string. You can use that information to confirm that the string header is pointing to the original string.
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
for i := 0; i < sh.Len; i++ {
sp := (*byte)(unsafe.Pointer(sh.Data + uintptr(i)))
fmt.Printf("%p = %c\n", sp, *sp)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论