Article: The Lost Art of Structure Packing
Nick Treleaven
nick at geany.org
Mon Nov 2 13:58:29 UTC 2026
On Monday, 2 November 2026 at 12:22:38 UTC, Ogion wrote:
> Maybe we need some way to allow the compiler rearrange the
> order of struct fields to optimally pack them, like it does
> with classes?
>
> ```D
> struct S {
> align (auto):
> byte a; // placed at offset 0
> int b; // placed at offset 4
> byte[3] c; // placed at offset 1
> }
> ```
BTW making `S` a class doesn't pack its fields like that with dmd
AFAICT. I get field offsets 16, 20, 24. However, if `a` and `c`
are declared next to each other then it does. In fact, if you do
that then the struct will get packed the same as well.
> I’d even argue that auto-packing should be the default option.
That might be optimal for memory size, but not necessarily for
speed AIUI. However, having the option makes sense, assuming a
compiler does do this for classes already.
More information about the Digitalmars-d
mailing list