-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Incorrect layout in extern struct
#16633
Comments
Zig gives each type a "default"/"native"/"natural" alignment per target configuration. (C compilers do the same.) It's possible that Zig doesn't mimic your system C compiler with these alignments (making it C-ABI incompatible), which would be a bug. Therefore I think it's more likely that the structs are actually defined differently in C, with something like |
@rohlem many many thanks. I was omitting the code was packed with I can confirm this is not a bug. Sadly, some alignments are platform dependent as well, and some structs have their own alignment. A sad afternoon waits for me. The extra bytes are emitted by the library. I assume the structs are not up to date with the internal data structures. |
For future reference, after very long and expensive days of trying too hard, I couldn't find/brute force an elegant way to derive the same alignment of the C code from Zig. My stop-loss insurance was to create a file with lots of Then I used the differences in the alignment to explicitly set the alignment for each field for each struct of the library https://github.com/menduz/zig-steamworks/blob/158780e84088b742894da9a4b0e778a12cb470e6/src/main.zig#L963 |
@menduz Interesting finds. If |
Zig Version
0.11.0-dev.4308+417b92f08
Steps to Reproduce and Observed Behavior
Proof of concept and reproduction code: https://zig.godbolt.org/z/34vx4MGbe
Based on my understanding of the semantics of
extern struct
, I'd expect to have a C compatible memory layout. Paddings are being added as it were noextern
.Context
Which partially solves the problem of reading the data, but it doesn't solve the interoperability. More importantly, the reader is not recursive, making nested structs not work either.
Here is a C memory dump of a struct
0100000052b0554e00008601
, the PoC code outputs the following:Expected Behavior
I'd expect to have compatible memory layouts between C abi and extern struct
The text was updated successfully, but these errors were encountered: