<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Learning-to-Code on despatches</title><link>https://icle.es/tags/learning-to-code/</link><description>Recent content in Learning-to-Code on despatches</description><generator>Hugo</generator><language>en</language><lastBuildDate>Fri, 27 Jun 2025 10:30:12 +0100</lastBuildDate><atom:link href="https://icle.es/tags/learning-to-code/index.xml" rel="self" type="application/rss+xml"/><item><title>init &amp; deinit in Zig</title><link>https://icle.es/2025/04/27/init-deinit-in-zig/</link><pubDate>Sun, 27 Apr 2025 17:01:57 +0100</pubDate><guid>https://icle.es/2025/04/27/init-deinit-in-zig/</guid><description>&lt;p>When I was ten, my grandmother passed away. As was custom where we lived, my
family moved into her house - a full household with uncles and aunts. I stayed
there for about nine months.&lt;/p>
&lt;p>My youngest uncle was a Computing Studies teacher at a local college. He also
taught private classes at home. I couldn’t get enough.&lt;/p>
&lt;p>I don’t know what most ten-year-olds dream about, but my dream was to learn C. I
saved ₹300 - a lot of money in 1994 - to buy a book called &lt;em>Encyclopedia C&lt;/em>. I
read it cover to cover, understood maybe 10% of it, and reread it years later to
pick up more.&lt;/p>
&lt;p>But I never actually programmed in C.&lt;/p></description><content:encoded><![CDATA[<p>When I was ten, my grandmother passed away. As was custom where we lived, my
family moved into her house - a full household with uncles and aunts. I stayed
there for about nine months.</p>
<p>My youngest uncle was a Computing Studies teacher at a local college. He also
taught private classes at home. I couldn’t get enough.</p>
<p>I don’t know what most ten-year-olds dream about, but my dream was to learn C. I
saved ₹300 - a lot of money in 1994 - to buy a book called <em>Encyclopedia C</em>. I
read it cover to cover, understood maybe 10% of it, and reread it years later to
pick up more.</p>
<p>But I never actually programmed in C.</p>
<h2 id="the-long-detour">The Long Detour</h2>
<p>Life took me through a range of other languages instead: Prolog (strangely, what
my school machines had), Visual Basic, ASP, PHP, Java, Python, JavaScript, Go,
Rust. I tinkered with C++ when messing with game engines, but never quite got
around to C.</p>
<p>Some of the early fears carried through - memory management was intimidating:
<code>malloc</code>, <code>free</code>, and in C++, <code>new</code> and <code>delete</code>.</p>
<p>Over time, I began to overcome the fear of systems languages - first through
Java, then Go. But I never revisited C, and I never quite got over the fear of
manual memory management.</p>
<p>In hindsight, these concepts weren’t really designed for a ten-year-old to
grasp. It makes sense that they felt out of reach.</p>
<h2 id="allocator-confusion">Allocator Confusion</h2>
<p>Zig had me curious for a while, and some recent health issues gave me the space
to explore it. I’ve been building a small game in Zig, and it’s been feeding
that original desire to learn C - just with a bit more clarity.</p>
<p>I was still intimidated by memory allocation though, and went as far as I could
without using an allocator.</p>
<p>Eventually, I had to use one. I understood the convention of <code>init</code> and
<code>deinit</code>, and the idea of allocators in general.</p>
<p>But I was confused about how <code>deinit</code> worked in the context of <code>ArenaAllocator</code>.</p>
<blockquote>
<p>A little learning is a dangerous thing</p>
<p>&ndash; Alexander Pope</p></blockquote>
<p>If the arena frees memory for you, wouldn’t calling <code>deinit</code> on individual
objects inside it risk double-freeing?</p>
<p>If I skip calling <code>deinit</code> on a struct that normally needs it, will I miss other
cleanup tasks?</p>
<p>And does this mean the arena allocator isn’t a true drop-in replacement if I
have to skip <code>deinit</code>?</p>
<p>At the time, the answer wasn’t clear - and I couldn’t find documentation that
resolved it.</p>
<h2 id="clarity">Clarity</h2>
<p>Thankfully, the
<a href="https://ziggit.dev/t/deinit-and-arena-allocator/9856">super friendly folk at Ziggit</a>
helped clarify things.</p>
<h3 id="arenaallocator-handles-it"><code>ArenaAllocator</code> handles it</h3>
<p><code>ArenaAllocator</code> <em>is</em> a drop-in replacement for other allocators. If you
<code>deinit</code> objects using memory from the arena, and those objects try to free that
memory, the operation is effectively (though not exactly) a no-op. There’s no
risk of double-free.</p>
<blockquote>
<p>It’s always safe to free / destroy memory from an arena, as long as you treat
it as freed of course. But it won’t actually be released until the arena is
deinit’ed.</p>
<p>&ndash; <a href="https://ziggit.dev/u/mnemnion/summary">mnemnion</a></p></blockquote>
<p>Even if some memory is freed manually before the arena is cleared, it’s handled
safely.</p>
<p>I was overthinking it.</p>
<h3 id="deinit-should-still-be-called"><code>deinit</code> should still be called</h3>
<p><code>deinit</code> exists for cleanup logic - not just memory. It should always be called
where appropriate, regardless of the allocator. That part doesn’t change.</p>
<h3 id="further-learning">Further learning</h3>
<p>Zig also encourages a light touch. One of the impacts of this is that the object
itself should not hold on to the allocator in the <code>init</code> and use it in <code>deinit</code>.
It could, but the better way would be to accept the <code>allocator</code> in both the
<code>init</code> and the <code>deinit</code>.</p>
<p>This convention is further reinforced by
<a href="https://ziglang.org/download/0.14.0/release-notes.html#Embracing-Unmanaged-Style-Containers">zig deprecating the <code>managed</code> variants of collections</a></p>
<h2 id="thanks">Thanks</h2>
<p>In many ways, this was about cleaning up more than just memory.</p>
<p>Special thanks to the helpful folks at <a href="https://ziggit.dev/">ziggit.dev</a>.</p>
<h2 id="references">References</h2>
<ul>
<li><a href="https://ziggit.dev/t/deinit-and-arena-allocator/9856">Full discussion on ziggit.dev</a></li>
<li><a href="https://github.com/ziglang/zig/blob/d92649da80a526f2e2b2f220c05b81becf4fa627/lib/std/heap/arena_allocator.zig#L253-L267">Implementation of <code>ArenaAllocator</code></a></li>
</ul>]]></content:encoded></item></channel></rss>