It’s not compiling. The SIGTRAP is being triggered by the compiler.
To be honest, I’m not sure why, if it is obvious that having a slice of anyopaque is not allowed, Zig doesn’t throw a compilation error. Also, I’m unsure about where exactly the SIGTRAP happens.
Regarding the example. Thank you for showing how to cast pointers (didn’t reach that part yet:)). I couldn’t get it work though with t being const:
const std = @import("std");
const T = packed struct {
f: u8,
};
const U = packed struct {
f: u4,
g: u4,
};
const t: T = .{ .f = 0xAB };
const u: *const U = @ptrCast(&t);
pub fn main() !void {
std.log.debug("{any}", .{@import("builtin").target.cpu.arch.endian()});
std.log.debug("{X}", .{t.f});
std.log.debug("{X} {X}", .{ u.f, u.g });
}
Throws:
file.zig:17:34: error: dereference of '*align(1:0:1) const u4' exceeds bounds of containing decl of type 'u0'
std.log.debug("{X} {X}", .{ u.f, u.g });
~^~
However, when I get rid of pointer const-ness like this:
var t: T = .{ .f = 0xAB };
const u: *U = @ptrCast(&t);
I get the expected:
debug: builtin.Endian.little
debug: AB
debug: B A