Defer Reference Implementation for C
The C language shouldn&#x27;t need a defer statement keyword, because it&#x27;s so trivial to implement using an asm() macro that overwrites the return address.<p>Using a macro is more succinct:<p><pre><code>    const char *s = gc(xasprintf(&quot;%s&#x2F;%s&quot;, dir, name));
</code></pre>
Than what&#x27;s being proposed:<p><pre><code>    char *s = xasprintf(&quot;%s&#x2F;%s&quot;, dir, name);
    defer free(s);
</code></pre>
See this x86 reference implementation of defer() and gc().  <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;jart&#x2F;aed0fd7a7fa68385d19e76a63db687ff" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;jart&#x2F;aed0fd7a7fa68385d19e76a63db687f...</a> That should just work with GCC and Clang. That code is originally from the Cosmopolitan C Library (<a href="https:&#x2F;&#x2F;github.com&#x2F;jart&#x2F;cosmopolitan" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jart&#x2F;cosmopolitan</a>) so check it out if you like the Gist and want more!<p>Please note the macro operates at function call boundaries rather than block scoped. I consider that a feature since it behaves sort of like a memory pool. Having side effects at the block scope level requires changing compilers, the language itself, and it would cause several important gcc optimization passes to be disabled in places where it&#x27;s used.