0

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.

BelMat
  • 93
  • 8
  • 1
    I don't see what's wrong with your code (aside from try on sort, it's obviously incorrect). How do you allocate items? – sigod Mar 02 '23 at 17:20
  • Yeah, so it was an error on how the items where allocated, I was checking mostly the code on the sort. I left one index free and fixed that and now the issue is gone. Thanks! – BelMat Mar 03 '23 at 03:07

1 Answers1

0

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.

BelMat
  • 93
  • 8