去内存中的切片地址

时间:2018-05-05 17:56:52

标签: go slice padding

我在64位操作系统上运行以下代码,得到的结果是每个切片的地址之间的差异为32Byte(0xc42008a060 - 0xc42008a040 0xc42008a040 - 0xc42008a020)。我想它应该是24Byte,因为切片的大小是24B而对齐是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或更大。我只是对切片和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是数据阵列的地址,第二个是切片的长度,第三个是切片的容量。但最后的8B似乎是空的。最后一个8B用于什么?填充?

1 个答案:

答案 0 :(得分:1)

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

Example

尺寸符合预期 但是atomic package does mention

  

变量或分配的结构,数组或切片中的第一个单词可以依赖于64位对齐。

请参阅this thread

  

引用是“全局变量或分配的第一个单词   结构或切片可以依赖于64位对齐。“   关键词是“已分配”,而非“结构或切片”。

     

通过new或复合文字分配的结构或切片中的第一个单词将是64位对齐的。
  切片或数组中的结构本身不会分配,并且除了unsafe.AlignOf之外,不保证特定的对齐。