Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsafe: Allocated object address changed #56209

Closed
cocotyty opened this issue Oct 13, 2022 · 2 comments
Closed

unsafe: Allocated object address changed #56209

cocotyty opened this issue Oct 13, 2022 · 2 comments

Comments

@cocotyty
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.19.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOOS="linux"

What did you do?

https://go.dev/play/p/XvMkBVE5b7Y

package main

import (
	"log"
	"unsafe"
)

func main() {
	h := new(Array)
	ptr := uintptr(unsafe.Pointer(h))
	show(h)
	ptr2 := uintptr(unsafe.Pointer(h))
	show(h)
	ptr3 := uintptr(unsafe.Pointer(h))
	log.Println(ptr, ptr2, ptr3)
}

func show(t *Array) {
	log.Println((*t)[1])
}

type Array [399]int

What did you expect to see?

These three pointer value are the same.

What did you see instead?

The first value is not the same as the second and the third one.

@cocotyty cocotyty changed the title Allocated object address changed unsafe: Allocated object address changed Oct 13, 2022
@mateusz834
Copy link
Contributor

mateusz834 commented Oct 13, 2022

./main.go:9:10: new(Array) does not escape
./main.go:11:6: ... argument does not escape
./main.go:11:6: (*t)[1] escapes to heap
./main.go:13:6: ... argument does not escape

Looks like h is on the stack. And probably log.Println() causes the stack to grow (so the pointers change).

@cocotyty
Copy link
Author

./main.go:9:10: new(Array) does not escape
./main.go:11:6: ... argument does not escape
./main.go:11:6: (*t)[1] escapes to heap
./main.go:13:6: ... argument does not escape

Looks like h is on the stack. And probably log.Println() causes the stack to grow (so the pointers change).

Thank you for your help 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants