Skip to content

Instantly share code, notes, and snippets.

@felixge
Created October 18, 2018 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixge/6ae41cece4b8b9755d1b06ab1740b7ff to your computer and use it in GitHub Desktop.
Save felixge/6ae41cece4b8b9755d1b06ab1740b7ff to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
f1 := &Foo{}
f2 := &Foo{}
fmt.Printf("%p = %p\n", f1, f2)
b1 := &Bar{}
b2 := &Bar{}
fmt.Printf("%p != %p\n", b1, b2)
}
type Foo struct{}
type Bar struct{ Field string }
$ go run main.go
0x1157c08 = 0x1157c08
0xc42000e1e0 != 0xc42000e1f0
WTH? Why wouldn't the pointers be different in both cases?
@felixge
Copy link
Author

felixge commented Oct 19, 2018

@mholt yeah, the spec seems to leave it open to the implementation. The latter seems to return a single fixed address for all 0-sized allocations:

	if size == 0 {
		return unsafe.Pointer(&zerobase)
	}

See:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment