-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
allow align(0) on struct fields #3802
Comments
Is there a reason why someone wouldn't first reach for embedding a packed struct inside a normal struct for bitflags before trying this? I do see the general appeal of making align(0) well defined, an in the manner you've described, but specifically for flags, there are already, seemingly simpler, more controlled ways of accomplishing the task. Overall, I think that when operating at the bit level, one's mind is probably wanting data laid out carefully rather than letting the compiler take the wheel. |
A packed struct has a well-defined in-memory layout. If you do not need this, then it is better to avoid it, for plenty of reasons:
However if you need well-defined in-memory layout, that's fine, that's what packed structs are for. |
Shouldn't |
This is a sister issue to #3133. Packed structs offer a way to explicitly provide the layout of a struct in memory. But non-packed structs also can offer something powerful: the ability to say that you do not need a field to be aligned to a byte boundary.
Because structs do not guarantee field order, Zig is allowed to put
a
,c
,d
, ande
fields into 1 byte, making the entire struct only 2 bytes, rather than 5.The compromise here is that pointers to align(0) fields have pointer metadata in the type that prevents it from being coerced to a "normal" pointer for that type. This the same thing as taking a pointer of a non-byte-aligned packed struct field.
However this would be quite useful for flags, where one typically does not need a pointer to an individual flag. Rather, one typically accesses flags from a base pointer, which would work just fine with align(0) flag fields.
The text was updated successfully, but these errors were encountered: