Hello, It's day 3 of messing with Zig, and I need to make some allocations in my program.
I saw this code online:
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer {
const deinit_status = gpa.deinit();
//fail test; can't try in defer as defer is executed after we return
if (deinit_status == .leak) expect(false) catch @panic("TEST FAIL");
}
And I'm having some question perhaps someone might be able to anwser them.
First question is regarding the first line of code. So Zig has this concept of returning types in function, and looking into the code of std.heap.GeneralPurposeAllocator(), it returns a struct with a bunch of stuff inside.
The code then (I assume) instantiates that type var gpa = std.heap.GeneralPurposeAllocator(.{}){};, so my first question is, is the code doing exactly that, and is any function ran by default, kinda like how in C++ a default constructor is called? Or is the "constructor" the part where we do std.heap.GeneralPurposeAllocator(.{})? It accepts a config as argument, so is it also a way to construct the instance? Pass values to the function that returns the type or setting values in the {} after?
Second question is, Is calling the deinit() a common pattern for types in Zig?