#pointer alignment guarantees

1 messages · Page 1 of 1 (latest)

trim holly
#

Zig allows you to specify a pointer alignment in the type, e.g. *align(4) u8 is a u8 which is guaranteed to be 4-byte aligned. If the alignment isn't specified, it defaults to the type's natural alignment - so yes, on 64-bit platforms at least a standard *u64 is guaranteed to be 8-byte aligned

#

Of course, if you do hacky enough stuff you can break that guarantee, but the language does enforce it with safety-checked UB wherever it can

trim holly
#

np

#

if you want to double-check a type's natural alignment you can use @alignOf - e.g. chuck @compileLog(@alignOf(u64)) in your code and it'll print out the natural alignment of u64 during compilation

#

maybe you would also find it useful to assert that alignment at comptime, so your build errors out on platforms that don't have the alignment you're relying on. e.g. comptime std.debug.assert(@alignOf(u64) >= 8)