如何在 zig 中创建二维容器数组?

问题描述

我正在尝试在 Zig 中分配 HashMap(u32,u1) 的二维数组:

fn alloc2d(comptime t: type,m: u32,n: u32,allocator: *Allocator) callconv(.Inline) ![][]t {
    const array = try allocator.alloc([]t,m);
    for (array) |_,index| {
        array[index] = try allocator.alloc(t,n);
    }
    return array;
}

fn free2d(comptime t: type,array: [][]t,allocator: *Allocator) callconv(.Inline) void {
    for (array) |_,index| {
        allocator.free(array[index]);
    }
    allocator.free(array);
}

test "Alloc 2D Array" {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = &gpa.allocator;
    defer _ = gpa.deinit();

    const HashSet = std.AutoHashMap(u32,u1);
    var array = try alloc2d(*HashSet,4,allocator);
    defer free2d(*HashSet,array,allocator);

    for (array) |_,i| {
        for (array[i]) |_,j| {
            array[i][j] = &(HashSet.init(allocator));
        }
    }
    defer {
        for (array) |_,i| {
            for (array[i]) |_,j| {
                array[i][j].deinit();
            }
        }
    }
}

然而,当我测试它时,调试器抛出了一个错误

谁能告诉我发生了什么以及如何解决它?

非常感谢!

解决方法

我正在查看您的代码,乍一看,它似乎符合您的预期;我不太确定您为什么要向函数传递 *HashSet 而不仅仅是 HashSet

...

var array = try alloc2d(HashSet,4,allocator);
defer free2d(HashSet,array,allocator);

for (array) |_,i| {
    for (array[i]) |_,j| {
        array[i][j] = HashSet.init(allocator);
    }
}

... 

事实上,如果你这样做,一切都会如你所愿。

也就是说,我看不出你的版本不工作的原因,所以我仔细研究了一下,发现似乎正在发生的事情是你的 array 中的每一项都是使用相同的地址初始化,即 &(HashSet.init(allocator)) 每次都返回相同的地址。我认为这就是 deinit 调用出现段错误的原因,内存被多次释放。

如果您手动初始化数组中的每个元素,例如[0][0] = (HashSet.init(allocator)...etc 似乎一切正常。我不完全确定这里发生了什么,但可能有某种编译器优化在起作用,可能与泛型的工作方式有关。希望其他人能给出更好的答案。

有点不相关,但是 Zig 的一个简洁特性,您可以通过引用迭代切片,这有时更容易阅读:

for (array) |*outer| {
    for (outer.*) |*item| {
        item.* = <something>
    }
}