最近在学习go slice 碰到了一个问题,在本地跑下面测试的时候,slice的size是24B,但是发现两个slice的地址总是相差32B (`0xc42008a040 - 0xc42008a020` `0xc42008a060-0xc42008a040`)。而int的size是8B, 两个变量地址差距正好是8B。测试代码如下
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("sl2 addr %p, align %d, size %d\n", &sl2, unsafe.Alignof(sl2), unsafe.Sizeof(sl2))
fmt.Printf("sl3 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:
sl1 addr 0xc42008a020, align 8, size 24
sl2 addr 0xc42008a040, align 8, size 24
sl3 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
slice的align=8,size=24,但sl1, sl2, sl3地址差距都是32B,然后使用下面测试代码打印出来对应地址的值
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是slice的长度,第三个8B是slice容量,最后一个8B是什么不太明白。代码是跑在64-bits的mac上的,指向数组地址及表示长度和容量的int都是占8位,size也是24B,感觉最后一个8B不应该是因为填充吧。哪位大神能帮忙看一下。谢谢
这个应该是内存对齐的问题,虽然unsafe.Alignof(i1)的值是8, 但slice结构在内存还是以16字节对齐。我自己做了一个测试,说明是字节对齐的问题。![2018-05-10 10-46-43屏幕截图.png](https://static.studygolang.com/180510/a12a938fdb5f32a93d91d86e2ae15723.png)
这里内存对齐也是8字节, 但各变量在内存中的地址分布和slice结构一样, 按16字节对齐的。
#1