|
|
|
|
|
by Zambyte
250 days ago
|
|
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... |
|