Zig Master: Introduction to Allocators
ฝัง
- เผยแพร่เมื่อ 21 พ.ย. 2024
- Help me create more content like this!
www.paypal.com...
Allocating on the heap is performed via allocators in Zig. This is one of the distinctive features of Zig as a language, and in this episode we take a whirlwind tour of the basic use of an allocator. We also see some sample functions that take allocators to produce their output.
The code: codeberg.org/d...
Relevant Links:
ziglang.org/do...
Thumbnail artwork courtesy of: www.deviantart...
thanks for the great video about zig allocators.
Thanks for the content ❤
hi whats the difference between zig master and zig in depth series? both the names sound similar
Zig in Depth was made when Zig version 0.11 was the stable version. Aside from the build system and some command changes, the language itself is mostly the same so the videos are still relevant. Zig Master is covering the latest versions of Zig.
@@dudethebuilder thanks, love the series, thanks for creating it.
Confused about the str_out. Didn't we already free that inside the asBytes function?
In asBytes, the ArrayList collects the bytes in its items array allocated on the heap. When we call toOwnedSlice, that array of bytes on the heap is detached from the list and returned from asBytes. So when the defer deinit for the list kicks in, the list is already empty. It's now the responsibility of the caller of asBytes to free the returned bytes. Basically, the list just serves as a convenient temporary mechanism to collect the bytes on the heap.
@@dudethebuilder I think he's talking about the `const code_points_out_2 = try asCodePoints(str_out, &buf);` line. Although the result is the same in the end, it would have been more consistent to use *str_in* as the first argument, i.e. `const code_points_out_2 = try asCodePoints(str_in, &buf);`. After all, you did use its length to define the size of the buffer that's used as the second argument. In any case, thanks for the tutorial!
Good eye! That's totally true. I fixed the code and pushed the new version. Thank you for clarifying.