Zero sized value storage

189 views
Skip to first unread message

shan...@gmail.com

unread,
May 25, 2021, 5:10:46 PM5/25/21
to golang-dev

Hi all, there was a question on Reddit that has generated a discussion where I am confused, and am hoping someone can clear up for me.

https://www.reddit.com/r/golang/comments/njejut/two_distinct_zerosized_variables_question/

The confusion I have is multiple pointers of different types (assuming that's what is meant by one of the comments) being able to point to the same address.

If the premise is correct, then I presume that there is some global "zero size" constant that pointers of different types can address?

If not, then how does it work (*much* appreciation will be given to links in the source that show me how it's actually done)

Or, if the premise is completely wrong, how /are/ zero sized values dealt with on the heap?

Also, I note in my answer on that page that there seems to be an optimisation for struct{} on the heap, but this doesn't seem to be happening for the stack, and I also note that I am *guessing* for the stack, can someone give me the definitive answer (or how to look it up)

Keith Randall

unread,
May 25, 2021, 5:51:20 PM5/25/21
to shan...@gmail.com, golang-dev
On Tue, May 25, 2021 at 2:10 PM shan...@gmail.com <shan...@gmail.com> wrote:

Hi all, there was a question on Reddit that has generated a discussion where I am confused, and am hoping someone can clear up for me.

https://www.reddit.com/r/golang/comments/njejut/two_distinct_zerosized_variables_question/

The confusion I have is multiple pointers of different types (assuming that's what is meant by one of the comments) being able to point to the same address.

If the premise is correct, then I presume that there is some global "zero size" constant that pointers of different types can address?


Exactly. See https://golang.org/src/runtime/malloc.go , lines 840-841 and lines 910-912.
 
If not, then how does it work (*much* appreciation will be given to links in the source that show me how it's actually done)

Or, if the premise is completely wrong, how /are/ zero sized values dealt with on the heap?

Also, I note in my answer on that page that there seems to be an optimisation for struct{} on the heap, but this doesn't seem to be happening for the stack, and I also note that I am *guessing* for the stack, can someone give me the definitive answer (or how to look it up)

If you have a struct like:
struct {
   x int
   y struct{}
}

Then taking the address of y will give you a pointer into the heap (or stack, if that's where that struct is allocated).
Not sure about allocating a zero-sized object on the stack. I'd have to check the code.

--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/26fb0740-4ced-4ae4-ab25-ab3478830371n%40googlegroups.com.

Ian Lance Taylor

unread,
May 25, 2021, 5:51:40 PM5/25/21
to shan...@gmail.com, golang-dev
On Tue, May 25, 2021 at 2:10 PM shan...@gmail.com <shan...@gmail.com> wrote:
>
First I'll note that the language spec says
(https://golang.org/ref/spec#Comparison_operators) "Pointers to
distinct zero-size variables may or may not be equal" and
(https://golang.org/ref/spec#Size_and_alignment_guarantees) "A struct
or array type has size zero if it contains no fields (or elements,
respectively) that have a size greater than zero. Two distinct
zero-size variables may have the same address in memory."

The implementation for heap values is quite simple and can be found at
https://golang.org/src/runtime/malloc.go?#L910:

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

For stack based values at least some of code is at
https://golang.org/src/cmd/compile/internal/gc/walk.go?#L837. In
general look for "zerobase" under cmd/compile/internal and you should
find the relevant code.

Ian

shan...@gmail.com

unread,
May 25, 2021, 6:02:06 PM5/25/21
to golang-dev
Thank you both for your responses, that is exactly what I needed

(Note: I'm aware that this is implementation specific, but I feel it's also important that anyone that happens upon this thread is also made aware of that fact)
Reply all
Reply to author
Forward
0 new messages