Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
go中结构的内存分配_Go_Memory_64 Bit - Fatal编程技术网

go中结构的内存分配

go中结构的内存分配,go,memory,64-bit,Go,Memory,64 Bit,我最近遇到了一个解释如何为结构分配内存的例子。我明白了,为了确保连续内存分配,我们在为变量分配内存时添加了填充,没有填充就不会得到连续内存。所以我在我的64位计算机上测试了各种组合,发现网站上的结果与我的计算机不匹配。就本案而言: type S2 struct { a string b bool e bool d int32 f bool c string } 大体上,下面的代码给出48作为变量的大小 y := S2{"q", true, tru

我最近遇到了一个解释如何为结构分配内存的例子。我明白了,为了确保连续内存分配,我们在为变量分配内存时添加了填充,没有填充就不会得到连续内存。所以我在我的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))
但这与它的预期不同。为什么会观察到这种行为?我希望这不是我的电脑本身的问题。 编辑:从逻辑上讲,字段d和f之间不需要填充

我还运行了下面的代码来确保

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

使用32位的机器,所以我怀疑同样的可以复制到那里

您对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

那个网站是错误的。不知道为什么你会质疑一些非官方网站的实际实现。你必须问运行该网站的人为什么它是错误的。但从逻辑上讲,在字段d和d之间添加空格是没有意义的f@NannanAV字体为什么没有意义?int32的对齐保证是4,因此它不能从字节18开始。字节16到23在64位机器上是连续的,因此它可以!参考评论@nanav:你的数学没有意义。这是错误的。