Zero-sized data type

275 views
Skip to first unread message

Richiise Nugraha

unread,
Oct 12, 2022, 12:00:54 PM10/12/22
to golang-nuts
Hi, I am looking for zero-sized data type, something like Flexible Array Member.
The size of `struct {}` is indeed zero, but for what reason when it's inside a struct with another member say (https://go.dev/play/p/DpydJIke7dS):

type C struct {
    Pre   uint64
    Inner struct{}
}

That struct sized for 16, while the sum of all member sizes is only 8, it's like there's hidden padding/align.

Thanks!
Nugraha

Jan Mercl

unread,
Oct 12, 2022, 12:26:53 PM10/12/22
to Richiise Nugraha, golang-nuts
IINM, if the last field of a Go struct has zero size the struct is
extended by at least one byte to prevent taking the address of the
zero sized field that could be outside of the allocated memory area
for the instance of C. (That could, for example, make the precision garbage collector consider the following memory block reachable when it is not.)

If that's the case then the definition above is actually laid out as

type C struct {
        Pre uint64
        Inner byte
}

Note that the flexible array member concept of C is in most cases not compatible with memory managed
by the Go runtime as described in the documentation for package unsafe. It should be fine in memory
not managed by the Go runtime, though.

-j

Axel Wagner

unread,
Oct 12, 2022, 1:27:14 PM10/12/22
to Richiise Nugraha, golang-nuts
You can re-order the fields to get the desired effect:

On Wed, Oct 12, 2022 at 6:01 PM Richiise Nugraha <rich...@gmail.com> wrote:
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2f7254a8-379a-4dc2-bf10-70ee7a209af2n%40googlegroups.com.

Marcel Huijkman

unread,
Oct 13, 2022, 1:21:38 PM10/13/22
to golang-nuts
What is the usage of that zero-sized data type? Can you give an example where that Inner field has any usage?

Richiise Nugraha

unread,
Oct 13, 2022, 1:45:34 PM10/13/22
to golang-nuts
The field can't be reordered.
I want to use zero-sized data type to mark offset and has no effect on struct size, I can take the address of it and use it like:
```go
c := ...
cmd := (*[SIZE]byte)(unsafe.Pointer(&c.Inner))
```
I am using this on mmap-ed memory.
I've found a workaround to do the same thing by adding `c` pointer with it's data size

Konstantin Khomoutov

unread,
Oct 20, 2022, 8:54:46 AM10/20/22
to Richiise Nugraha, golang-nuts
On Thu, Oct 13, 2022 at 10:45:33AM -0700, Richiise Nugraha wrote:

> The field can't be reordered.
> I want to use zero-sized data type to mark offset and has no effect on
> struct size, I can take the address of it and use it like:
> ```go
> c := ...
> cmd := (*[SIZE]byte)(unsafe.Pointer(&c.Inner))
> ```
> I am using this on mmap-ed memory.
> I've found a workaround to do the same thing by adding `c` pointer with
> it's data size

IIUC, you cannot use flexible arrays in Go - please see [1] and the
Go isse #20275 [2].

1. https://stackoverflow.com/a/73660093/720999
2. https://github.com/golang/go/issues/20275

Reply all
Reply to author
Forward
0 new messages