3 ms·
Go is only allowed to move variables if it knows there are not pointers to them, so only the compiler can take this decision. The Go runtime is very much not al
by tsimionescu 5y ago
Go is only allowed to move variables if it knows there are not pointers to them, so only the compiler can take this decision. The Go runtime is very much not allowed to move variables.
- Dylan16807 5y agoI don't think this is right. Every source I can find says that Go does not currently move heap variables, but it reserves the right to, and code that assumes no movement is wrong. I also found code that demonstrates stack variables being moved despite a pointer to them, but it's version dependent so I can't test it. When a variable is moved, then obviously pointers to it need to be updated, but that's not an intractable problem. Looking further, Go doesn't even provide an API for pinning objects right now[1]. You have to do weird workarounds to allocate static objects, or write code that assumes nothing on the heap moves and will explode if that ever changes. [1] https://github.com/golang/go/issues/46787 https://github.com/golang/go/issues/46787