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...

ความคิดเห็น • 9

  • @thilina91
    @thilina91 หลายเดือนก่อน +1

    thanks for the great video about zig allocators.

  • @rodriidamn98
    @rodriidamn98 4 หลายเดือนก่อน +2

    Thanks for the content ❤

  • @YuruCampSupermacy
    @YuruCampSupermacy 4 หลายเดือนก่อน +3

    hi whats the difference between zig master and zig in depth series? both the names sound similar

    • @dudethebuilder
      @dudethebuilder  4 หลายเดือนก่อน +4

      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.

    • @YuruCampSupermacy
      @YuruCampSupermacy 4 หลายเดือนก่อน +3

      @@dudethebuilder thanks, love the series, thanks for creating it.

  • @binitrupakheti4246
    @binitrupakheti4246 4 หลายเดือนก่อน +4

    Confused about the str_out. Didn't we already free that inside the asBytes function?

    • @dudethebuilder
      @dudethebuilder  4 หลายเดือนก่อน +2

      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.

    • @noel6857
      @noel6857 4 หลายเดือนก่อน +1

      @@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!

    • @dudethebuilder
      @dudethebuilder  4 หลายเดือนก่อน +1

      Good eye! That's totally true. I fixed the code and pushed the new version. Thank you for clarifying.