From 28ebd12e92d9542d20bf8966ecab5a27f84812c2 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 19 Dec 2020 12:19:24 -0600 Subject: [PATCH] typing out the stuff on the website --- overflow.zig | 4 ++++ runtime_overflow.zig | 4 ++++ undefined.zig | 15 +++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 overflow.zig create mode 100644 runtime_overflow.zig create mode 100644 undefined.zig diff --git a/overflow.zig b/overflow.zig new file mode 100644 index 0000000..4cd0a5e --- /dev/null +++ b/overflow.zig @@ -0,0 +1,4 @@ +test "integer overflow at compile time" { + const x: u8 = 255; + const y = x + 1; +} diff --git a/runtime_overflow.zig b/runtime_overflow.zig new file mode 100644 index 0000000..082f96a --- /dev/null +++ b/runtime_overflow.zig @@ -0,0 +1,4 @@ +test "integer overflow at runtime" { + var x: u8 = 255; + x += 1; +} diff --git a/undefined.zig b/undefined.zig new file mode 100644 index 0000000..7ba1938 --- /dev/null +++ b/undefined.zig @@ -0,0 +1,15 @@ +const std = @import("std"); + +pub fn main() void { + // if you remove this line, the program does not compile because it detects + // the overflow at compile time. A little confusing that it's not + // @setCompileTimeSafety but that's ok. + @setRuntimeSafety(false); + + var x: u8 = 255; + x += 1; // <-- this is an overflow + + // this prints 0 on my machine but is apparently undefined. I wonder what + // it prints on other machines? + std.debug.print("X: {}\n", .{x}); +}