logoalt Hacker News

rvrbyesterday at 8:29 PM2 repliesview on HN

`extern` and `packed` container types have well defined layouts. a regular `struct` is an "auto" layout - and the compiler can and will rearrange whenever it wants.

if you need a well defined layout, use `extern`. if your struct makes sense to represent as an integer, use `packed`. I think it is often ill advisable to use `packed` otherwise.

you can explore this yourself on the Type info returned from @TypeInfo(T):

https://ziglang.org/documentation/master/std/#std.builtin.Ty...

https://ziglang.org/documentation/master/std/#std.builtin.Ty...

https://ziglang.org/documentation/master/std/#std.builtin.Ty...


Replies

LexiMaxyesterday at 8:44 PM

To wit: https://ziglang.org/documentation/master/#extern-struct

> An extern struct has in-memory layout matching the C ABI for the target.

Zig is really good at speaking the C ABI of the target, but the upshot seems to be that it appears there is no stable Zig-native ABI.

If I'm correct, I wonder if there are plans to settle on a stable ABI at some point in the future. I do know that in other languages the lack of a stable ABI is brought up as a downside, and although I've been burned by C++ ABI stability too many times to agree, I can understand why people would want one.

show 2 replies
dnauticsyesterday at 10:20 PM

in practice, as long as you match the version and release mode, it's fine (though you are playing with fire). I pass raw pointers to zig structs/unions/etc from the zig compiler into a dynamically loaded .so file (via dlload) and as long as my .so file is compiled with the same compiler as the parent (both LLVM, in my case) it's peachy keen.

show 1 reply