gpt4 book ai didi

转到内存中的 slice 地址

转载 作者:IT王子 更新时间:2023-10-29 02:09:52 25 4
gpt4 key购买 nike

我在 64 位操作系统上运行了以下代码,得到的结果是每个 slice 的地址之间的差异是 32Byte(0xc42008a060 - 0xc42008a040 0xc42008a040 - 0xc42008a020)。我猜应该是24Byte,因为slice的大小是24B,align是8。就像int64的例子。

package main

import (
"fmt"
"unsafe"
)
func main() {
var sl1 []int
var sl2 []int
var sl3 []int
fmt.Printf("slice:\n")
fmt.Printf("sl1 addr %p, align %d, size %d\n", &sl1, unsafe.Alignof(sl1), unsafe.Sizeof(sl1))
fmt.Printf("sl1 addr %p, align %d, size %d\n", &sl2, unsafe.Alignof(sl2), unsafe.Sizeof(sl2))
fmt.Printf("sl1 addr %p, align %d, size %d\n", &sl3, unsafe.Alignof(sl3), unsafe.Sizeof(sl3))

var i1 int
var i2 int
var i3 int
fmt.Printf("int:\n")
fmt.Printf("i1 addr %p, align %d, size %d\n", &i1, unsafe.Alignof(i1), unsafe.Sizeof(i1))
fmt.Printf("i2 addr %p, align %d, size %d\n", &i2, unsafe.Alignof(i2), unsafe.Sizeof(i2))
fmt.Printf("i3 addr %p, align %d, size %d\n", &i3, unsafe.Alignof(i3), unsafe.Sizeof(i3))
}

输出:

slice:
slice:
sl1 addr 0xc42008a020, align 8, size 24
sl1 addr 0xc42008a040, align 8, size 24
sl1 addr 0xc42008a060, align 8, size 24
int:
i1 addr 0xc42007c020, align 8, size 8
i2 addr 0xc42007c028, align 8, size 8
i3 addr 0xc42007c030, align 8, size 8

一开始我猜可能是其他东西占用了额外的8B,然后我重新运行了很多次,有时我修改了一些代码,地址也发生了变化,但我总是发现差异是32B或更大。我对 slice 和 struct padding 知之甚少,对此非常困惑。谁能帮我。非常感谢

--------更新另一个测试------

我像这样通过点打印值

package main

import (
"fmt"
"unsafe"
)

func main() {

arr := [5]int{1, 2, 3, 4, 5}
s1 := arr[0:1]
s2 := arr[0:2]

fmt.Printf("arr addr %p\n", &arr)
fmt.Printf("s1 addr %p, align %d, size %d\n", &s1, unsafe.Alignof(s1), unsafe.Sizeof(s1))
fmt.Printf("s2 addr %p, align %d, size %d\n", &s2, unsafe.Alignof(s2), unsafe.Sizeof(s2))

//var spInt *int64 = (*int64)(unsafe.Pointer(sp))
fmt.Printf("\ns1:\n")
sp := &s1
fmt.Printf("address: %v\n", unsafe.Pointer(uintptr(unsafe.Pointer(sp))))
fmt.Println(fmt.Sprintf("+ 0(arr): 0x%x", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp))))))
fmt.Printf("+ 8(len): %d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp)) + uintptr(8))))
fmt.Printf("+16(cap): %d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp)) + uintptr(16))))
fmt.Printf("+24(unknow): %d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp)) + uintptr(24))))

fmt.Printf("\ns2:\n")
sp = &s2
fmt.Printf("address: %v\n", unsafe.Pointer(uintptr(unsafe.Pointer(sp))))
fmt.Println(fmt.Sprintf("+ 0(arr): 0x%x", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp))))))
fmt.Printf("+ 8(len): %d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp)) + uintptr(8))))
fmt.Printf("+16(cap): %d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp)) + uintptr(16))))
fmt.Printf("+24(unknow): %d\n\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(sp)) + uintptr(24))))
}

我得到了结果:

arr addr 0xc420018150
s1 addr 0xc42000a060, align 8, size 24
s2 addr 0xc42000a080, align 8, size 24

s1:
address: 0xc42000a060
+ 0(arr): 0xc420018150
+ 8(len): 1
+16(cap): 5
+24(unknow): 0

s2:
address: 0xc42000a080
+ 0(arr): 0xc420018150
+ 8(len): 2
+16(cap): 5
+24(unknow): 0

第一个8B是数据数组的地址,第二个是slice的长度,第三个是slice的容量。但是最后的8B好像是空的。最后一个8B是做什么用的?用于填充?

最佳答案

您可以使用项目 tyranron/golang-sizeof.tips 验证您的假设及其在线页面 golang-sizeof.tips

Example

大小符合预期。
但是 atomic package does mention :

The first word in a variable or in an allocated struct, array, or slice can be relied upon to be 64-bit aligned.

参见 this thread :

The quote is "The first word in a global variable or in an allocated struct or slice can be relied upon to be 64-bit aligned."
The key phrase there is "allocated", not "struct or slice."

The first word in a struct or slice that is allocated via new or via a composite literal will be 64-bit aligned.
A struct inside a slice or array is not allocated by itself, and no particular alignment is guaranteed beyond that of unsafe.AlignOf.

关于转到内存中的 slice 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50192467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com