A wrong wording in Zig doc?

The packed struct serction zig doc says:

Packed structs have the same alignment as their backing integer, however, overaligned pointers to packed structs can override this:

Should “overaligned” be “underalgined” here? After all, 4 is under @alignOf(S) (8).

As I understand that word in context as: over-riden aligmentoveraligned ¯\_(ツ)_/¯

1 Like

I agree think the wording/example is not quite right. Also it says “pointers” can change the alignment, but what’s really changing the alignment in the example is the local itself.

Maybe should be more like…

const std = @import("std");
const expectEqual = std.testing.expectEqual;

const S = packed struct {
    a: u32,
    b: u32,
};
test "underaligned pointer to packed struct" {
    var foo: S = .{ .a = 1, .b = 2 };
    const ptr: *align(4) S = &foo;
    const ptr_to_b = &ptr.b;
    try expectEqual(2, ptr_to_b.*);
}

Or even throw an align(16) on the local to drive the point home that you can change the alignment of a packed struct.

1 Like

If this is a rule for any types, not packed structs specific, my opinion is this sub-section is unnecessary.

const std = @import("std");
const expectEqual = std.testing.expectEqual;

test "underaligned pointer to u64" {
    var foo: u64 align(2) = 789;
    const ptr: *align(2) u64 = &foo;
    try expectEqual(789, ptr.*);
}