一尘不染

空结构片的地址

go

我对empty structs 有一个基本问题,并试图了解以下两个不同的输出,以获取两个切片的后备数组元素的地址:

a := make([]struct{}, 10)
b := make([]struct{}, 20)
fmt.Println("&a == &b", &a == &b)
fmt.Println("&a[0] == &b[0]", &a[0] == &b[0])

上面的代码片段返回

&a == &b false
&a[0] == &b[0] true

但是,请考虑以下略有变化的代码段:

a := make([]struct{}, 10)
b := make([]struct{}, 20)
fmt.Println(a[0], &a[0])
fmt.Println("&a == &b", &a == &b)
fmt.Println("&a[0] == &b[0]", &a[0] == &b[0])

上面的代码片段返回

{} &{}
&a == &b false
&a[0] == &b[0] false

有人可以解释上述差异的原因吗?谢谢!

[跟进] 进行以下修改:

package main

import "fmt"

type S struct{}

func (s *S) addr() { fmt.Printf("%p\n", s) }

func main() {
    a := make([]S, 10)
    b := make([]S, 20)
    fmt.Println(a[0], &a[0])
    fmt.Println("&a == &b", &a == &b)
    fmt.Println("&a[0] == &b[0]", &a[0] == &b[0])
    //a[0].addr()
    //b[0].addr()
}

仍返回相同的输出:

{} &{}
&a == &b false
&a[0] == &b[0] false

尽管取消注释方法调用,但返回

{} &{}
&a == &b false
&a[0] == &b[0] true
0x19583c // ==> [depends upon env]
0x19583c // ==> [depends upon env]

阅读 188

收藏
2020-07-02

共1个答案

一尘不染

在深入研究之前,请了解根据规范,程序对于大小为零的值产生的地址是否相等或不同,都是正确的,因为规范仅声明它们 可以 相同,但不要求它们是相同的。相同。

规格:尺寸和对齐方式保证:

如果结构或数组类型不包含大小大于零的字段(或元素),则其大小为零。两个不同的零大小变量在内存中 可能 具有相同的地址。

因此,您所体验的是实现细节。有关决策的更多细节和因素,以下解释仅对您的具体示例有效并足够:

在您的第一个示例中,切片的后备数组的地址仅在main()函数内部使用,它们不会逸出到堆中。您打印的只是地址比较的结果。这些只是bool值,不包括地址值。因此,编译器选择对a和的后备数组使用相同的地址b

在您的第二个示例中,后备数组的地址(更具体地说是后备数组的某些元素的地址)在函数 外部
使用main(),它们被传递到fmt.Println()函数内部并在函数内部使用,因为您还将打印这些地址。

我们可以通过将-gcflags '-m'参数传递给Go工具来“证明”这一点,要求它打印转义分析的结果。

在第一个示例中,将代码保存到中play.go,运行go run -gcflags '-m' play.go命令,输出为:

./play.go:10:14: "&a == &b" escapes to heap
./play.go:10:29: &a == &b escapes to heap
./play.go:11:14: "&a[0] == &b[0]" escapes to heap
./play.go:11:38: &a[0] == &b[0] escapes to heap
./play.go:8:11: main make([]struct {}, 10) does not escape
./play.go:9:11: main make([]struct {}, 20) does not escape
./play.go:10:26: main &a does not escape
./play.go:10:32: main &b does not escape
./play.go:10:13: main ... argument does not escape
./play.go:11:32: main &a[0] does not escape
./play.go:11:41: main &b[0] does not escape
./play.go:11:13: main ... argument does not escape
&a == &b false
&a[0] == &b[0] true

我们可以看到,地址不会逃脱。

go run -gcflags '-m' play.go在第二个示例中运行,输出为:

./play.go:10:15: a[0] escapes to heap
./play.go:10:20: &a[0] escapes to heap
./play.go:10:20: &a[0] escapes to heap
./play.go:8:11: make([]struct {}, 10) escapes to heap
./play.go:11:14: "&a == &b" escapes to heap
./play.go:11:29: &a == &b escapes to heap
./play.go:12:14: "&a[0] == &b[0]" escapes to heap
./play.go:12:38: &a[0] == &b[0] escapes to heap
./play.go:9:11: main make([]struct {}, 20) does not escape
./play.go:10:13: main ... argument does not escape
./play.go:11:26: main &a does not escape
./play.go:11:32: main &b does not escape
./play.go:11:13: main ... argument does not escape
./play.go:12:32: main &a[0] does not escape
./play.go:12:41: main &b[0] does not escape
./play.go:12:13: main ... argument does not escape
{} &{}
&a == &b false
&a[0] == &b[0] false

正如可以看到,a[0]&a[0]逃逸到堆,所以背衬阵列a是动态分配的,因此将具有比的不同的地址b的。

让我们进一步“证明”这一点。让我们来修改你的第二个例子,有一个第三个变量c,其地址也不会被打印出来,让我们比较bc

a := make([]struct{}, 10)
b := make([]struct{}, 20)
c := make([]struct{}, 30)
fmt.Println(a[0], &a[0])
fmt.Println("&a == &b", &a == &b)
fmt.Println("&a[0] == &b[0]", &a[0] == &b[0])
fmt.Println("&b == &c", &b == &c)
fmt.Println("&b[0] == &c[0]", &b[0] == &c[0])

go run -gcflags '-m' play.go在此上运行,输出为:

./play.go:11:15: a[0] escapes to heap
./play.go:11:20: &a[0] escapes to heap
./play.go:11:20: &a[0] escapes to heap
./play.go:8:11: make([]struct {}, 10) escapes to heap
./play.go:12:14: "&a == &b" escapes to heap
./play.go:12:29: &a == &b escapes to heap
./play.go:13:14: "&a[0] == &b[0]" escapes to heap
./play.go:13:38: &a[0] == &b[0] escapes to heap
./play.go:14:14: "&b == &c" escapes to heap
./play.go:14:29: &b == &c escapes to heap
./play.go:15:14: "&b[0] == &c[0]" escapes to heap
./play.go:15:38: &b[0] == &c[0] escapes to heap
./play.go:9:11: main make([]struct {}, 20) does not escape
./play.go:10:11: main make([]struct {}, 30) does not escape
./play.go:11:13: main ... argument does not escape
./play.go:12:26: main &a does not escape
./play.go:12:32: main &b does not escape
./play.go:12:13: main ... argument does not escape
./play.go:13:32: main &a[0] does not escape
./play.go:13:41: main &b[0] does not escape
./play.go:13:13: main ... argument does not escape
./play.go:14:26: main &b does not escape
./play.go:14:32: main &c does not escape
./play.go:14:13: main ... argument does not escape
./play.go:15:32: main &b[0] does not escape
./play.go:15:41: main &c[0] does not escape
./play.go:15:13: main ... argument does not escape
{} &{}
&a == &b false
&a[0] == &b[0] false
&b == &c false
&b[0] == &c[0] true

由于仅&a[0]被印刷而不&b[0]也不&c[0],从而&a[0] == &b[0]false&b[0] == &c[0]true

2020-07-02