<?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>Forem: Yair Lenga</title>
    <description>The latest articles on Forem by Yair Lenga (@yairlenga).</description>
    <link>https://forem.com/yairlenga</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%2F1298982%2F76a53c65-5034-4416-9fd8-c9a52568a42e.png</url>
      <title>Forem: Yair Lenga</title>
      <link>https://forem.com/yairlenga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yairlenga"/>
    <language>en</language>
    <item>
      <title>Optimizing Chained strcmp Calls for Speed and Clarity</title>
      <dc:creator>Yair Lenga</dc:creator>
      <pubDate>Tue, 14 Apr 2026 10:35:42 +0000</pubDate>
      <link>https://forem.com/yairlenga/optimizing-chained-strcmp-calls-for-speed-and-clarity-11d8</link>
      <guid>https://forem.com/yairlenga/optimizing-chained-strcmp-calls-for-speed-and-clarity-11d8</guid>
      <description>&lt;h2&gt;
  
  
  From memcmp and bloom filters to 4CC encoding for small fixed-length string comparisons
&lt;/h2&gt;

&lt;p&gt;I've been working on an article to describe a small performance issues with a pattern I've seen multiple times - long chain of if statements based on strcmp. This is the equivalent of switch/case on string value (which is not supported in C).&lt;/p&gt;

&lt;p&gt;The code implemented critical business logic, based on currency codes (ISO 3-letter code), and an as-of date. Similar to:&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="n"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;model_ccy_lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;asof&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;model_param&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Major Currencies&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&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;"USD"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&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;"EUR"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="c1"&gt;// Asia-Core&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&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;"CNY"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;strcmp&lt;/span&gt;&lt;span class="p"&gt;(&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;"HKD"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code couldn’t be refactored into a different structure (for non-technical reasons), so I had to explore few approaches to keep the existing structure - without rewrite/reshape of the logic. I tried few tings - like &lt;code&gt;memcmp&lt;/code&gt;, small filters, and eventually packing the strings into 32-bit values (“4CC”-style) and letting the compiler work with integer compares.&lt;/p&gt;

&lt;p&gt;I was able to revise the code into cleaner form, which outperform the original code by 10X. This was achieved with a series of experiments, some of them helped, some of them did not yield improvements, but hinted at other directions.&lt;/p&gt;




&lt;h4&gt;
  
  
  Full Article: Medium (No Paywall):
&lt;/h4&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://medium.com/@yair.lenga/optimizing-chained-strcmp-calls-for-speed-and-clarity-without-refactoring-b57035b78f18" rel="noopener noreferrer"&gt;Optimizing Chained &lt;code&gt;strcmp&lt;/code&gt;Calls for Speed and Clarity&lt;/a&gt;
&lt;/h3&gt;




&lt;p&gt;The final implementation looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;model_ccy_lookup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&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;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;asof&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;model_param&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Major Currencies&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;CCY_IN&lt;/span&gt;&lt;span class="p"&gt;(&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;"USD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"EUR"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="c1"&gt;// Asia-Core&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;CCY_IN&lt;/span&gt;&lt;span class="p"&gt;(&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;"CNY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"HKD"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nf"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the CCY_IN was implemented as a series of integer compare, using the FourCC encoding = replacing each fixed-size &lt;code&gt;strcmp&lt;/code&gt; with a call to CCY_EQ macro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#define CCY_EQ(x, ccy) (*(int *)x == *(int*) ccy )
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;I’m also trying a slightly different writing style than usual - a bit more narrative, focusing on the path (including the dead ends), not just the final result.&lt;/p&gt;

&lt;p&gt;If you have a few minutes, I’d really appreciate feedback (comment here or on Medium) on two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does the technical content hold up?&lt;/li&gt;
&lt;li&gt;Is the presentation clear, or does it feel too long / indirect?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interested to hear on other ideas/approach for this problem as well.&lt;/p&gt;

</description>
      <category>performance</category>
      <category>c</category>
      <category>coding</category>
      <category>programming</category>
    </item>
    <item>
      <title>Safer Casting in C — With Zero Runtime Cost</title>
      <dc:creator>Yair Lenga</dc:creator>
      <pubDate>Tue, 07 Apr 2026 07:58:10 +0000</pubDate>
      <link>https://forem.com/yairlenga/safer-casting-in-c-with-zero-runtime-cost-26bc</link>
      <guid>https://forem.com/yairlenga/safer-casting-in-c-with-zero-runtime-cost-26bc</guid>
      <description>&lt;p&gt;C gives us two types of casting: &lt;strong&gt;Implicit casting&lt;/strong&gt; — happens automatically in expressions and function calls, and &lt;strong&gt;Explicit casting&lt;/strong&gt; — with the cast operator &lt;code&gt;(T) v&lt;/code&gt; (e.g. &lt;code&gt;(int) x&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Both are powerful - and both introduce risks - making subtle, hard-to-detect bugs easy to introduce.&lt;/p&gt;

&lt;p&gt;I've been looking for safer way to use casts in C, and I came up with simple idea: replace &lt;code&gt;(T) v&lt;/code&gt; with function like macros, similar in spirit to SQL's &lt;code&gt;CONVERT(type, value)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A Small Example (same behavior, better readability):&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="c1"&gt;// before, It it:&lt;/span&gt;
&lt;span class="c1"&gt;//     (*((long *) buf)) + 1 OR&lt;/span&gt;
&lt;span class="c1"&gt;//     *((long *) (buf + 1 )) OR&lt;/span&gt;
&lt;span class="c1"&gt;//     *(((long *) buf) + 1)&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* after */&lt;/span&gt;
&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;CAST_PTR1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Full Article (Medium - no paywall):
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://medium.com/@yair.lenga/safer-casting-in-c-with-zero-runtime-cost-making-casts-visible-auditable-and-harder-to-misuse-331b3a3a8090" rel="noopener noreferrer"&gt;Safer Casting in C — With Zero Runtime Cost&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I tried a simple approach: replace (T)v with function-like macros such as CAST_VAL, CAST_PTR, and CAST_PTR1,  and UNCONST. The idea isn’t to make C type-safe, but to make casts explicit, structured, and easier to audit. Some basic checks can be enforced at compile time (using gcc/clang extensions), and everything still compiles down to the same code — no runtime cost.&lt;/p&gt;

&lt;p&gt;In practice, this shifts casts from “hidden syntax” to something more visible and intentional. For example, separating value casts from pointer casts, and explicitly handling things like removing &lt;code&gt;const&lt;/code&gt;, makes questionable conversions stand out immediately in code review.&lt;/p&gt;

&lt;p&gt;Curious if others have tried something similar, or if you rely mostly on compiler warnings (&lt;code&gt;-Wconversion&lt;/code&gt;, &lt;code&gt;-Wcast-*&lt;/code&gt;) and static analysis for this. Does this feel useful, or just adding noise on top of C’s existing model?&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>coding</category>
      <category>linux</category>
    </item>
    <item>
      <title>Stack vs malloc: real-world benchmark shows 2–6x difference</title>
      <dc:creator>Yair Lenga</dc:creator>
      <pubDate>Wed, 01 Apr 2026 12:02:40 +0000</pubDate>
      <link>https://forem.com/yairlenga/stack-vs-malloc-real-world-benchmark-shows-2-6x-difference-4601</link>
      <guid>https://forem.com/yairlenga/stack-vs-malloc-real-world-benchmark-shows-2-6x-difference-4601</guid>
      <description>&lt;p&gt;Usually, we assume that malloc is fast—and in most cases it is. &lt;br&gt;
However, sometimes "reasonable" code can lead to very unreasonable performance.&lt;/p&gt;

&lt;p&gt;In a previous post, I looked at using stack-based allocation (VLA / fixed-size) for temporary data, and another on estimating available stack space to use it safely.&lt;/p&gt;

&lt;p&gt;This time I wanted to measure the actual impact in a realistic workload.&lt;/p&gt;

&lt;h3&gt;
  
  
  Full Article (Medium - no paywall):
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://blog.stackademic.com/temporary-memory-isnt-free-allocation-strategies-and-their-hidden-costs-159247f7f856" rel="noopener noreferrer"&gt;Stack vs malloc: real-world benchmark shows 2–6x difference&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I built a benchmark based on a loan portfolio PV calculation, where each loan creates several temporary arrays (thousands of elements each). This is fairly typical code-clean, modular, nothing unusual.&lt;/p&gt;

&lt;p&gt;I compared:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;stack allocation (VLA)&lt;/li&gt;
&lt;li&gt;heap per-loan (malloc/free)&lt;/li&gt;
&lt;li&gt;heap reuse&lt;/li&gt;
&lt;li&gt;static (baseline)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Results:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;stack allocation stays very close to optimal&lt;/li&gt;
&lt;li&gt;heap per-loan can be ~2.5x slower (glibc) and up to ~6x slower (musl)&lt;/li&gt;
&lt;li&gt;even optimized allocators show pattern-dependent behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9c5p54k71rfo7f1ktzxw.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9c5p54k71rfo7f1ktzxw.webp" alt="Stack Heap Allocation Comparison" width="720" height="392"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main takeaway for me: allocation cost is usually hidden—but once it's in the hot path, it really matters.&lt;/p&gt;

&lt;p&gt;Curious how others approach temporary workspace in performance-sensitive code.&lt;/p&gt;

</description>
      <category>c</category>
      <category>performance</category>
      <category>programming</category>
      <category>memory</category>
    </item>
    <item>
      <title>Avoiding malloc for Small Strings in C With Variable Length Arrays (VLAs)</title>
      <dc:creator>Yair Lenga</dc:creator>
      <pubDate>Thu, 19 Mar 2026 13:31:00 +0000</pubDate>
      <link>https://forem.com/yairlenga/avoiding-malloc-for-small-strings-in-c-with-variable-length-arrays-vlas-2ig7</link>
      <guid>https://forem.com/yairlenga/avoiding-malloc-for-small-strings-in-c-with-variable-length-arrays-vlas-2ig7</guid>
      <description>&lt;p&gt;Many C programs allocate small temporary buffers using &lt;code&gt;malloc()&lt;/code&gt;, even when the buffer only lives for the duration of a function call.&lt;/p&gt;

&lt;p&gt;In many cases, &lt;strong&gt;stack allocation using VLAs&lt;/strong&gt; can be significantly faster because it avoids allocator overhead and improves cache locality.&lt;/p&gt;

&lt;p&gt;I wrote a deeper analysis including benchmarks and practical guidelines.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Full article (Medium — no paywall):&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://medium.com/@yair.lenga/avoiding-malloc-for-small-strings-in-c-with-variable-length-arrays-vlas-7b1fbcae7193" rel="noopener noreferrer"&gt;Using VLA for Faster Temporary Strings in C&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The article proposes &lt;strong&gt;conditional logic&lt;/strong&gt;: use a VLA when the requested size safely fits on the stack, and fall back to &lt;code&gt;malloc()&lt;/code&gt; otherwise. Small macros hide the decision so the code remains compact and readable.&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="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;test1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&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;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&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;s2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Create char *result - pointing to heap/stack&lt;/span&gt;
  &lt;span class="n"&gt;FLEX_STR_INIT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;strlen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Use the buffer&lt;/span&gt;
  &lt;span class="n"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FLEX_STR_BUF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s2&lt;/span&gt;&lt;span class="p"&gt;))&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;"result(%zu)=%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;FLEX_STR_SIZE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Release buffer (NOOP is VLA was used)&lt;/span&gt;
  &lt;span class="n"&gt;FLEX_STR_FREE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For small strings this approach can reduce allocation overhead dramatically.&lt;/p&gt;

</description>
      <category>c</category>
      <category>performance</category>
      <category>memory</category>
      <category>programming</category>
    </item>
    <item>
      <title>Estimating Remaining Stack Space in a C Program</title>
      <dc:creator>Yair Lenga</dc:creator>
      <pubDate>Tue, 17 Mar 2026 23:40:13 +0000</pubDate>
      <link>https://forem.com/yairlenga/estimating-remaining-stack-space-in-a-c-program-4dh7</link>
      <guid>https://forem.com/yairlenga/estimating-remaining-stack-space-in-a-c-program-4dh7</guid>
      <description>&lt;p&gt;Many programmers believe there is &lt;strong&gt;no reliable way to know how much stack space remains&lt;/strong&gt; in a running C program. Because of this uncertainty, stack allocation techniques such as &lt;strong&gt;VLA&lt;/strong&gt;s or &lt;code&gt;alloca()&lt;/code&gt; are often considered unsafe unless the buffer size is extremely small.&lt;/p&gt;

&lt;p&gt;On modern Unix systems (Linux, BSD, macOS), the runtime environment actually exposes &lt;strong&gt;enough information to estimate the remaining stack space with reasonable accuracy&lt;/strong&gt;. By combining stack limits with the current stack pointer, it is possible to determine how much space remains before reaching the guard page.&lt;/p&gt;

&lt;p&gt;I wrote a detailed explanation, including example code and a small utility function that estimates available stack space.&lt;/p&gt;

&lt;p&gt;Full article (Medium, NO paywall):&lt;br&gt;
&lt;a href="https://medium.com/@yair.lenga/how-much-stack-space-do-you-have-estimating-remaining-stack-in-c-on-linux-3c9513beabd8" rel="noopener noreferrer"&gt;How Much Stack Space Do You Have? Estimating Remaining Stack in C on Linux&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The article discuss 3 techniques:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;getrlimit()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;pthread_getattr_np()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GCC/CLANG constructor attribute.&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together these allow building a small helper API such as:&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="kt"&gt;size_t&lt;/span&gt; &lt;span class="nf"&gt;stack_remaining&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;StackAddr&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack_marker_addr&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;stack_low_mark&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;stack_low_mark&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;stack_base&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;safety_margin&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows programs to inspect available stack space at runtime and make better decisions when choosing between stack allocation and heap allocation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Have you ever needed to estimate stack usage in production code?&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>c</category>
      <category>systems</category>
      <category>linux</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
