Bitcast struct to flat array

Is it possible to directly typecast this example-struct to a flat array of f32’s?

pub const Set = struct {
    a: [6][32]f32,
    b: [8]f32,     
    c: [64]f32,   
    d: [64]f32,   
    e: f32,
    f: f32,
    g: [12]f32,
};
1 Like

Normal structs have no guaranteed memory layout, so this is not a good idea.
Even now Zig already sometimes reorders struct fields, and while that may not be relevant here, in the future Zig may choose more extreme options like e.g. adding hidden flags in debug mode to check for undefined. Then a cast would not be safe.

You could use an extern struct though. Then it would be safe to do this with a @ptrCast.

6 Likes

Oh of course! i forgot about that!
I will try that.

may be you can use packed struct?

packed structs can’t contain arrays (issue, langref).
Also every field in Set is 4-byte aligned so there’s no padding anyway. extern is the way to go here IMO.

If you want to be sure that there’s no accidental padding in case you ever change Set, you could add this:

comptime {
    assert(std.meta.hasUniqueRepresentation(Set));
}

EDIT: nevermind that function apparently returns false for floats, but it would work for e.g. u32 :slight_smile:

4 Likes

I currently do the manual thing: add up the field sizes and assert this equals @sizeOf in a test.

3 Likes

Yeah that’s effectively what std.meta.hasUniqueRepresentation does internally anyway

4 Likes