Safety in Zig: Spatial Memory Safety
Zig provides spatial memory safety. It can perform bounds checking at compile time, for comptime known values, and crash with appropriate stack trace for runtime values. This means it prevents out-of-bounds access to arrays etc. We’ll take a look at 2 simple examples to see how this works in practice. Example 1: Compile time known values const std = @import("std"); pub fn main() void{ var buffer = [_]u8{1, 2, 3, 4, 5}; const index: usize = 5; buffer[index] = 100; } In this example, we have an array buffer of 5 elements.
