-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Unpredictable behaviour regarding packed struct #15886
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
Comments
|
Should this behaviour happen regardless of whether the struct is annotated as extern or packed? You are correct, I did end up resolving this issue through use of Further the language reference does state under |
the bug is still likely valid yeah. it was likely extended to be a |
My guess as well, I was perhaps a bit ambitious with my mega integer haha |
Duplicate of #13480 |
Zig Version
0.11.0-dev.3218+b873ce1e0
Steps to Reproduce and Observed Behavior
I have observed some strange behaviour regarding packed structs which disappear completely when the packed annotation is removed. Consider the following code, which I've attempted to make relatively minimal in order to demonstrate this behaviour:
When creating the BitmapHeader packed struct through the
new
method as above, field values will turn into gibberish. For example, I have run the above code on aarch64 macOS and:file_header.file_size
andfile_header.offsert
fields are seemingly random values, like some bits in memory have been misinterpreted as their actual values.information_header.width
andinformation_header.height
fields are 0, not 1280 and 720 respectively as expected.These conditions are pretty fragile. If I directly place the literals 1280 and 720 into the width and height fields, the example works correctly. If I lift out all of the logic from the
new
method into themain
function, the example works correctly. If I remove an arbitrary number of fields from theInformationHeader
packed struct (such as the last two, as was the case when I was trying to minimise this example), funnily enough the example works correctly. If the packed struct annotation is removed from all structs in the example, it works correctly.Apologies if I'm butting against a part of the language I just don't understand, perhaps there are footguns with the packed struct that I need to learn. However, this seemed unintuitive to the point where I felt it was a bug.
Expected Behavior
That packed structs behave the same way as normal structs, at least in the contexts described above.
The above happens in debug, release fast and release safe modes.
The text was updated successfully, but these errors were encountered: