BelMat
BelMat

Reputation: 103

Zig 'expected error union type, found 'void''

I have an array of a struct and I am being unable to sort it. The compiler returns error: expected error union type, found 'void'.

var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!general_purpose_allocator.deinit());
const gpa = general_purpose_allocator.allocator();

var array: []Node = try gpa.alloc(Node, arr.len);
defer gpa.free(array);

... // Allocate the arr items to the new array

try std.sort.sort(Node, array[0..array.len], {}, cmp);
fn cmp(context: void, a: Node, b: Node) bool {
    _ = context;
    if (a.data > b.data) return true;
    return false;
}
const Node = struct {
    data: u32,
    value: ?u32,
};

if I remove the try of the std.sort.sort I get the error Segmentation fault at address 0x0 (...)

I am not very experienced in zig and clearly doing something wrong. (It is intended to use two arrays and not one). The code is running inside a test.

Upvotes: 0

Views: 1676

Answers (1)

BelMat
BelMat

Reputation: 103

Found the answer thanks to sigod's comment. It was indeed an issue on how the elements where being allocated and not on the sort as I thought.

Upvotes: 0

Related Questions