Any differences between Container Level Variables and local variables when alignCast

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

const array = [_]u32{ 0x11111111, 0x11111111 };
const bytes = mem.sliceAsBytes(array[0..]);

pub fn main() !void {
    const slice4 = bytes[1..5];
    var int_slice = mem.bytesAsSlice(u32, @alignCast(4, slice4));
    std.debug.print("{any}\n", .{@alignOf(@TypeOf(int_slice))});

    std.debug.print("{p}-{p}-{p}\n", .{ &int_slice[0], &array[0], &array[1] });
}

This program compile success, and output this in my macbook

8
u32@10073a3e5-u32@10073a3e4-u32@10073a3e8

However, when I declare array/bytes as local variables, this program will panic(as expected)

pub fn main() !void {
    const array = [_]u32{ 0x11111111, 0x11111111 };
    const bytes = mem.sliceAsBytes(array[0..]);
    const slice4 = bytes[1..5];
    var int_slice = mem.bytesAsSlice(u32, @alignCast(4, slice4));
    std.debug.print("{any}\n", .{@alignOf(@TypeOf(int_slice))});

    std.debug.print("{p}-{p}-{p}\n", .{ &int_slice[0], &array[0], &array[1] });
}
thread 299195 panic: incorrect alignment
/tmp/playground/zig-apps/src/align-demo.zig:9:37: 0x100b70c07 in main (align-demo)
    var int_slice = mem.bytesAsSlice(u32, @alignCast(4, slice4));
                                    ^

I wonder why the first program run successfully, slice4’s address is obviously not divided by 4.

@kristoff Would you mind have a look at this?

Uhmm I don’t have full first-hand knowledge, but I believe globals (variables put in the static memory of the executable) are required to have higher alignment according to one of the usual specs (C ABI, POSIX, …), but I don’t remember which one.