golang slice的内存分布

leshrac · · 1185 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

最近在学习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不应该是因为填充吧。哪位大神能帮忙看一下。谢谢

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1185 次点击  
加入收藏 微博
1 回复  |  直到 2018-05-10 10:48:54
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传