I have been using zig some days and start to learn about allocators. By what I know, zig doesn't have garbage collector, so something like the following code must bring an error:
const std = @import ("std");
const alloc_1 = std.heap.page_allocator;
pub fn main () !void {
const arr = try alloc_1.alloc (u8, 3);
arr[0] = 0;
}
I have using valgrind to track the memory leak with the next command valgrind --leak-check=full --track-origins=yes ./allocators and get this:
==23890== Memcheck, a memory error detector
==23890== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==23890== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==23890== Command: ./allocators
==23890==
==23890==
==23890== HEAP SUMMARY:
==23890== in use at exit: 0 bytes in 0 blocks
==23890== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==23890==
==23890== All heap blocks were freed -- no leaks are possible
==23890==
==23890== For lists of detected and suppressed errors, rerun with: -s
==23890== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
so basically the memory is cleared despite I don't dealloc it.
i don't think that valgrind works with llvm but idk you may prefer doing something like
ref: https://zig.guide/standard-library/allocators/