【问题标题】:Memory allocation for a struct in go [closed]go中结构的内存分配[关闭]
【发布时间】:2018-07-26 13:54:24
【问题描述】:

我最近遇到了http://golang-sizeof.tips/,它解释了如何为结构分配内存。我知道为了确保连续的内存分配,我们在为没有填充将不会获得连续内存的变量分配内存时添加填充。因此,我在我的 64 位计算机上测试了各种组合,发现网站上的结果与我的计算机上的结果不匹配。这是针对这种情况的:

type S2 struct {
    a string
    b bool
    e bool
    d int32
    f bool
    c string
}

主要是,下面的代码给了我48作为变量的大小。

y := S2{"q", true, true,2,true,"w"}
fmt.Println(unsafe.Sizeof(y))

但这与http://golang-sizeof.tips/?t=blahblah 的设想不同。为什么会观察到这种行为? (我希望这不是我的电脑本身的问题)。 编辑:从逻辑上讲,字段 df 之间不需要填充

为了确定,我还运行了以下代码。

fmt.Println(unsafe.Offsetof(y.a))
fmt.Println(unsafe.Offsetof(y.b))
fmt.Println(unsafe.Offsetof(y.e))
fmt.Println(unsafe.Offsetof(y.d))
fmt.Println(unsafe.Offsetof(y.f))
fmt.Println(unsafe.Offsetof(y.c))

结果:

0
16
17
20
24
32

play.golang.org 使用的是 32 位机器,所以我怀疑是否可以在那里复制!

【问题讨论】:

  • 那个网站是错误的。不知道为什么你会质疑一些非官方网站的实际实施。
  • 你得问问那个网站的运营者为什么错了。
  • 但从逻辑上讲,在字段 df 之间进行填充是没有意义的
  • @NannanAV:为什么没有意义? int32 的对齐保证是 4,所以它不能从第 18 个字节开始。
  • 字节 16 到 23 在 64 位机器上是连续的,因此可以!

标签: go memory 64-bit


【解决方案1】:

您计算的 48 字节对于 amd64 是正确的。

package main

import (
    "fmt"
    "unsafe"
)

type S2 struct { // align 16
    a string // size 16 = 8 + 8
    b bool   // size 1
    e bool   // size 1
    // pad size 2
    d int32 // size 4
    f bool  // size 1
    // pad size 7
    c string // size 16 = 8 + 8
}

func main() {
    y := S2{}
    fmt.Println(unsafe.Sizeof(y))

    fmt.Println(unsafe.Offsetof(y.a))
    fmt.Println(unsafe.Offsetof(y.b))
    fmt.Println(unsafe.Offsetof(y.e))
    fmt.Println(unsafe.Offsetof(y.d))
    fmt.Println(unsafe.Offsetof(y.f))
    fmt.Println(unsafe.Offsetof(y.c))

    fmt.Println(&y.a)
    fmt.Println(&y.b)
    fmt.Println(&y.e)
    fmt.Println(&y.d)
    fmt.Println(&y.f)
    fmt.Println(&y.c)
}

输出:

48
0
16
17
20
24
32
0xc000070150
0xc000070160
0xc000070161
0xc000070164
0xc000070168
0xc000070170

The Go Programming Language Specification : Size and alignment guarantees

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-20
  • 2022-08-20
  • 2021-11-08
  • 1970-01-01
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
相关资源
最近更新 更多