为了账号安全,请及时绑定邮箱和手机立即绑定

内存对齐问题

内存对齐问题

侃侃无极 2018-10-24 13:37:58
type W struct {     b byte     i int32     j int64}type W2 struct {     name string     age  int32}var w Wvar w2 W2 fmt.Println(unsafe.Sizeof(w), unsafe.Sizeof(w2))对于w变量我知道内存占用应该是16,但是w2为啥是24呢?求赐教
查看完整描述

1 回答

?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

import "fmt"import "unsafe"func main() {    var bs []byte
    fmt.Println("[]byte:", unsafe.Sizeof(bs))    var str string
    fmt.Println("string:", unsafe.Sizeof(str))    var i64 int64
    fmt.Println("int64:", unsafe.Sizeof(i64))    var i32 int64
    fmt.Println("int32:", unsafe.Sizeof(i32))    var stru struct{}
    fmt.Println("struct:", unsafe.Sizeof(stru))       
}

文档上这么写的

Sizeof takes an expression x of any type and returns the size in bytes of a hypothetical variable v as if v was declared via var v = x. The size does not include any memory possibly referenced by x. For instance, if x is a slice, Sizeof returns the size of the slice descriptor, not the size of the memory referenced by the slice.


查看完整回答
反对 回复 2018-10-24
  • 1 回答
  • 0 关注
  • 717 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信