4 comments

[ 15.9 ms ] story [ 564 ms ] thread
(comment deleted)
An excellent summary, which I wholeheartedly agree with
The "Debug mode" section does not mention the DebugAllocator[0], which does indeed crash with an error on the "Should be caught!" line. Try this:

    const std = @import("std");
    pub fn main() !void {
        var gpa: std.heap.DebugAllocator(.{}) = .init;
        defer _ = gpa.deinit();
        const allocator = gpa.allocator();
        //const allocator = std.heap.page_allocator;
    
        var buffer = try allocator.alloc(u8, 4);
        defer allocator.free(buffer);
        buffer[0] = 1;
        
        const new_buffer = try allocator.realloc(buffer, 8);
        defer allocator.free(new_buffer);
    
        buffer[0] = 99;
        std.debug.print("{}\n", .{buffer[0]});
    }
With the page_allocator, it will (incorrectly) print 99. With the DebugAllocator, it will crash with:

    Segmentation fault at address 0x7f4a78f80000
    prog.zig:14:11: 0x1158d55 in main (tmp_9CoXPDDeVU3O2FvW.zig)
        buffer[0] = 99;
              ^
[0] https://ziglang.org/documentation/0.15.1/std/#std.heap.debug...
I don't have enough experience with CPP and/or Clang but Zig compiles its whole build system and stdlib specifically for each project, so in that light I think the Zig compiler is quite fast.