golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.98k stars 17.67k forks source link

Question: The variable's address in stack may change when the stack expand ? #38406

Closed widon1104 closed 4 years ago

widon1104 commented 4 years ago

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

$ go version
widon@widon-deepin:~/golang/src/test/test1$ go version
go version go1.14.1 linux/amd64

Does this issue reproduce with the latest release?

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

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

What did you do?

I just want to ask a question, I read a golang book, the book says when the stack of a go routine expand the variable's address in stack may change. That's ready shock me. I don't know weather this statement right or wrong, if a variable allocated in stack then the stack expand and allocate stack space in another place the variable address in stack will change ? I think it's really dangerous If the variable address change beacuse the address may be store in a pointer, if the variable address change use "*" to get the value, the app will crash.

What did you expect to see?

What did you see instead?

agnivade commented 4 years ago

Hi @widon1104,

I think it's really dangerous If the variable address change beacuse the address may be store in a pointer, if the variable address change use "*" to get the value, the app will crash.

If that happens, the variable will escape to heap, and not be stack allocated anymore. And pointer checks will succeed.

Feel free to go through this thread here: https://twitter.com/empijei/status/1206718810025267200

Unlike other projects, we do not use the issue tracker for questions such as these. It is only used for bugs and feature proposals. I will close this issue, but please feel free to ask it in any of these forums below:

Thanks

ALTree commented 4 years ago

I think it's really dangerous If the variable address change beacuse the address may be store in a pointer, *if the variable address change use "" to get the value, the app will crash.**

This cannot happen. Go is a memory safe language. After the stack is copied to the new location, pointers local to the function are updated by the runtime.