zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

runtime_incorrect_pointer_alignment.zig (400B) - Raw


      1 const mem = @import("std").mem;
      2 pub fn main() !void {
      3     var array align(4) = [_]u32{ 0x11111111, 0x11111111 };
      4     const bytes = mem.sliceAsBytes(array[0..]);
      5     if (foo(bytes) != 0x11111111) return error.Wrong;
      6 }
      7 fn foo(bytes: []u8) u32 {
      8     const slice4 = bytes[1..5];
      9     const int_slice = mem.bytesAsSlice(u32, @as([]align(4) u8, @alignCast(slice4)));
     10     return int_slice[0];
     11 }
     12 
     13 // exe=fail