Large List Elements with 10 Billions

I have trouble to create a list with 10 billions elements in Zig, like the code below. How to fix this code?

const std = @import("std");

pub fn main() !void {
    const file_path = "large_array.data";
    const size = 10_000_000_000;

    var file = try std.fs.cwd().createFile(file_path, .{ .truncate = true });
    defer file.close();

    // Resize the file to 10 billion bytes
    try file.resize(size);

    // Memory-map the file
    const mapping = try std.fs.FileMapping.init(file, 0, size);
    defer mapping.deinit();

    const array = mapping.asBytes();
    for (array) |*byte| {
        byte.* = 1; // Initialize with `1`
    }

    // Access example
    std.debug.print("First element: {}\n", .{array[0]});
}

Do you generate this stuff with LLMs?

I am not aware of any std.fs.FileMapping, throwing these hallucinations at us gets a bit tiring.

There is std.posix.mmap for an example of how to use it see Mmap file - Zig cookbook

9 Likes