<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Attila Torda</title>
    <description>The latest articles on DEV Community by Attila Torda (@attilatorda).</description>
    <link>https://dev.to/attilatorda</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3054943%2F75a2355d-381e-455b-8020-b022a0e98e89.png</url>
      <title>DEV Community: Attila Torda</title>
      <link>https://dev.to/attilatorda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/attilatorda"/>
    <language>en</language>
    <item>
      <title>Finally: Memory Safety for C Without Rewriting Everything</title>
      <dc:creator>Attila Torda</dc:creator>
      <pubDate>Wed, 29 Apr 2026 23:16:02 +0000</pubDate>
      <link>https://dev.to/attilatorda/finally-memory-safety-for-c-without-rewriting-everything-3ldm</link>
      <guid>https://dev.to/attilatorda/finally-memory-safety-for-c-without-rewriting-everything-3ldm</guid>
      <description>&lt;p&gt;SaferCode is a header-only C library that brings modern memory safety patterns directly into your C code – with no new toolchains, no new languages, and no heavy dependencies.&lt;/p&gt;

&lt;p&gt;It gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Arena allocators – fast, linear, and leak-free&lt;/li&gt;
&lt;li&gt;RAII macros – automatic resource cleanup (yes, in C)&lt;/li&gt;
&lt;li&gt;String and string builder – length-prefixed, bound-checked strings&lt;/li&gt;
&lt;li&gt;Sentinel checks – detect stack buffer over/underflows at runtime&lt;/li&gt;
&lt;li&gt;Dangling pointer tracking – use-after-free? Caught.&lt;/li&gt;
&lt;li&gt;Memory file abstraction – unified file/memory I/O&lt;/li&gt;
&lt;li&gt;Structured logging &amp;amp; panic handling – consistent error flow&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of this without a C++ compiler or a runtime VM!&lt;/p&gt;

&lt;p&gt;Here's a quick taste:&lt;/p&gt;

&lt;p&gt;Arena allocator – no explicit free needed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"sc_arena.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;ScArena&lt;/span&gt; &lt;span class="n"&gt;arena&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;sc_arena_create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;arena&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc_arena_alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;arena&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc_arena_alloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;arena&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ... use memory ...&lt;/span&gt;

&lt;span class="n"&gt;sc_arena_reset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;arena&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// frees everything at once&lt;/span&gt;
&lt;span class="n"&gt;sc_arena_destroy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;arena&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Safe strings that know their length&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"sc_string.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;ScString&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc_string_new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello, "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;sc_string_append_cstr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"world!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sc_string_cstr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;  &lt;span class="c1"&gt;// "Hello, world!"&lt;/span&gt;
&lt;span class="n"&gt;sc_string_free&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No strcpy disasters, no missing null terminators.&lt;/p&gt;

&lt;p&gt;RAII-style cleanup&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"sc_raii.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;do_something&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sc_raii_scope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;FILE&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc_raii_register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fopen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"data.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sc_raii_cb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;fclose&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc_raii_register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;malloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Use f and buf...&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Both automatically freed/closed when scope ends&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why this is a good idea?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immediate improvement – You can start using it tomorrow in a single .c file. No build system overhaul.&lt;/li&gt;
&lt;li&gt;No dependency hell – It's just headers. Copy them into your project and go.&lt;/li&gt;
&lt;li&gt;Gradual adoption – Use one component (e.g., sc_string) without touching the rest.&lt;/li&gt;
&lt;li&gt;Learn once, use everywhere – The patterns (arena, RAII, etc.) are universal. You'll write better C even if you later drop SaferCode.&lt;/li&gt;
&lt;li&gt;Safety without performance loss – Arena allocators are faster than malloc/free. String builder reduces reallocation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Is it production-ready?&lt;/p&gt;

&lt;p&gt;The library is relatively new (version 0.x), but:&lt;/p&gt;

&lt;p&gt;It has unit tests (ctest).&lt;/p&gt;

&lt;p&gt;The API is stable-ish.&lt;/p&gt;

&lt;p&gt;The author clearly knows modern C safety techniques.&lt;/p&gt;

&lt;p&gt;For hobby projects, prototypes, or internal tools – absolutely yes. For safety-critical, million-line production code – test it thoroughly first, but the ideas are sound.&lt;/p&gt;

&lt;p&gt;What's missing?&lt;/p&gt;

&lt;p&gt;Like any young project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Documentation could be more comprehensive (but the headers are well commented).&lt;/li&gt;
&lt;li&gt;No official package manager integration yet (though easy to vendor).&lt;/li&gt;
&lt;li&gt;Some advanced patterns (e.g., generics) would need macros or code generation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The bottom line&lt;/p&gt;

&lt;p&gt;SaferCode doesn't turn C into Rust. But it gives you many of Rust's ergonomic safety patterns without leaving C.&lt;/p&gt;

&lt;p&gt;If you're tired of chasing memory bugs and want to write cleaner, safer C today, give it a try. Clone the repo, run the tests, and drop a header into your next project.&lt;/p&gt;

&lt;p&gt;GitHub: attilatorda/SaferCode&lt;/p&gt;

</description>
      <category>c</category>
      <category>memory</category>
      <category>advance</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Shift-To-Middle Array</title>
      <dc:creator>Attila Torda</dc:creator>
      <pubDate>Wed, 16 Apr 2025 09:08:02 +0000</pubDate>
      <link>https://dev.to/attilatorda/shift-to-middle-array-53jd</link>
      <guid>https://dev.to/attilatorda/shift-to-middle-array-53jd</guid>
      <description>&lt;p&gt;Hi all!&lt;/p&gt;

&lt;p&gt;I created a new way to implement lists. Essentially it's the same as array deques, however I've never seen this approach for lists. I wrote a publication (currently working on the second version) and there is freely available C++ implementation with benchmarks: &lt;a href="https://github.com/attilatorda/Shift-To-Middle_Array" rel="noopener noreferrer"&gt;https://github.com/attilatorda/Shift-To-Middle_Array&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Take a look at the readme.md for more details!&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>datastructures</category>
      <category>cpp</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
