#Is it safe to reinterpret memory with a packed union?

1 messages · Page 1 of 1 (latest)

quaint wharf
#

The zig language reference says

Bare unions cannot be used to reinterpret memory. For that, use @ptrCast, or use an extern union or a packed union which have guaranteed in-memory layout.
This means that I can, for example, safely do the following, right?

const Foo = packed union {
  .bar = packed struct { baz: u3 },
  .raw_bits = u3,
};
const foo = .{ .raw_bits = 0 };
std.debug.print("{}\n", .{foo.bar});
bronze depot
#

yes zeroLike