On Wed, Oct 12, 2022 at 6:01 PM Richiise Nugraha <richii...@gmail.com> wrote:
> 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. 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 -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAA40n-WLF1YZuT-cGyZ3JF6hyfUfKFgHiQbvGoHeoK4AuaQ9UA%40mail.gmail.com.