2 ms·
& doesn't always imply a value is on the heap. Escape analysis will ensure that pointers to the stack are safe. Here's an example with a bit of explanation: if
by anonacct38 5y ago
& doesn't always imply a value is on the heap. Escape analysis will ensure that pointers to the stack are safe.
Here's an example with a bit of explanation: if you pass a value to fmt.Println it will escape. The raw println builtin does not cause values to escape. So calling the first function twice and seeing the same address for the value strongly implies stack allocation while calling the 2nd function twice and getting different addresses implies heap allocation.
https://play.golang.org/p/PSb1wj1-x1c https://play.golang.org/p/PSb1wj1-x1c
- effdee 5y agoMinutes ago I was wondering about https://play.golang.org/p/9C0puRUstrP https://play.golang.org/p/9C0puRUstrP (via https://github.com/golang/go/issues/23440 https://github.com/golang/go/issues/23440). Thank you for explaining what's going on there! :) And btw, compiling the above example with -gcflags="-m" (which I learned about earlier today) proves you right.