Hi,
I am not able to understand why this piece of code is causing segmentation fault. Looking for an explanation.
const std = @import("std");
const Table = struct {
pages: [100]?*Page,
};
const Page = struct {
page: [12]*Row,
};
const Row = struct {
id: i32,
username: [32]u8,
email: [255]u8,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
const table = try allocator.create(Table);
for (0..100) |i| {
table.*.pages[i] = null;
}
std.debug.print("table.*.pages[0]: {any}.\n", .{table.*.pages[0]});
const allocated_page = try allocator.create(Page);
std.debug.print("allocated_page: {any}.\n", .{allocated_page});
table.*.pages[0] = allocated_page;
}
Output:
zig run demo.zig
table.*.pages[0]: null.
allocated_page: demo.Page{ .page = { demo.Row{ .id = Segmentation fault at address 0xaaaaaaaaaaaaaaaa
Panicked during a panic. Aborting.
zsh: abort zig run demo.zig
zig version: 0.13.0