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
Packed structs can't contain arrays #12547
Comments
Working as intended, see #10113 (comment) and the rest of the thread for reasoning. The error should explain it better though. |
The documentation regarding packed vs extern structs is poor in my opinion. I'll get around to improving it at some point, since it does appear to mislead people a lot. |
Is there any way to have something that works like a packed struct (i.e., guaranteed memory layout, no padding, can be used to reinterpret memory, ...) and can contain arrays? This seems like it should be a fairly common use case when decoding some binary data. Also, I'd argue that this is still a bug since the stage1 and stage2 compilers produce different results :) |
It's a bug in stage1, which won't be fixed, at least any time soon. Extern structs have a known memory layout and should be used in most cases, but where bit-packing is required, there is only the choice of packed structs, which can't contain arrays. There are a few discussions going on about solutions to this, but not sure how far they'll get. |
Ok, thanks. Then isn't this section of the documentation about extern structs incorrect? It contradicts your assertion that "Extern structs should be used in most cases" https://github.com/ziglang/zig/blob/master/doc/langref.html.in#L3272
|
Yes, I believe it is. |
So just to understand this correctly... At the moment there is no way at all to declare a structure with this layout in Zig struct asdf {
uint32_t a;
uint64_t b[4];
}; that has no padding between a and b on x86_64. Correct? |
@matrizzo It is possible const Asdf = extern struct {
a: u32,
b: [4]u64 align(1), // or even align(4)
};
test {
try std.testing.expectEqual(@sizeOf(u32) + 4 * @sizeOf(u64), @sizeOf(Asdf));
} |
Use an |
I'd recommend that you join a Zig community if you'd like further help. |
from packed struct docs "There is no padding between fields." Does that mean it should give back struct size as a total size of each type? |
a zig packed struct is not a c packed struct as has been explained above. a zig packed struct is for bit packing fields into an integer representation. and a |
Zig Version
0.10.0-dev.3659+e5e6eb983
Steps to Reproduce
Expected Behavior
The example compiles without errors with both
-fstage1
and with the default settings.Actual Behavior
The compiler complains that
[512]u8
can't be in a packed struct because "it has no guaranteed in-memory representation". This doesn't really make sense to me, isn't an u8 array supposed to be just a fixed-size sequence of bytes?When using the
-fstage1
the compiler doesn't complain.The text was updated successfully, but these errors were encountered: