<?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>Automatic Enum Handling in C — Parsing, Validating and Iterating.</title>
      <dc:creator>Yair Lenga</dc:creator>
      <pubDate>Mon, 04 May 2026 15:06:46 +0000</pubDate>
      <link>https://forem.com/yairlenga/automatic-enum-handling-in-c-parsing-validating-and-iterating-2ebk</link>
      <guid>https://forem.com/yairlenga/automatic-enum-handling-in-c-parsing-validating-and-iterating-2ebk</guid>
      <description>&lt;p&gt;In a previous post, &lt;a href="https://dev.to/yairlenga/automatic-enum-stringification-in-c-via-build-time-code-generation-115k"&gt;Automatic Enum Stringification in C via Build-Time Code Generation&lt;/a&gt;, I described how to extract enum labels and values directly from DWARF debug information at build time.&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;enum&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;C_NONE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;C_RED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;C_YELLOW&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;C_GREEN&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Request enum descriptor for e_color&lt;/span&gt;
&lt;span class="n"&gt;ENUM_DESCRIBE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e_color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="n"&gt;c&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;"Color=%s(%d)&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;ENUM_LABEL_OF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e_color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;c&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;In a follow-up article, I wrote about the next logical step: reverse conversion from symbolic name to value. This is useful when reading external input keyed by enum values: command-line arguments, configuration files, or user input.&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;ENUM_DESCRIBE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e_color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;parse_color&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;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;var&lt;/span&gt;&lt;span class="p"&gt;)&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;ENUM_PARSE_LABEL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e_color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;var&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;blockquote&gt;
&lt;p&gt;Medium Article (No Paywall): &lt;a href="https://medium.com/@yair.lenga/automatic-enum-handling-in-c-parsing-validating-and-iterating-76de362f9532" rel="noopener noreferrer"&gt;Automatic Enum Handling in C — Parsing, Validating and Iterating&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Beyond simple conversion label -&amp;gt; value, the collected metadata can be used to iterate over all enum values. This enables more advanced matching (e.g., partial or best match), or integration with external data keyed by enum values.&lt;/p&gt;

&lt;p&gt;In some cases, it can be useful to have the enum metadata available outside the C program. The extraction script can produce the information in TOML format - which can be read directly into Python or Java.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight toml"&gt;&lt;code&gt;&lt;span class="nn"&gt;[enum.currency]&lt;/span&gt;
&lt;span class="py"&gt;_.count&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;154&lt;/span&gt;
&lt;span class="py"&gt;_.symbol&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"enum_req_currency"&lt;/span&gt;
&lt;span class="py"&gt;_.anchor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"enum_type_currency"&lt;/span&gt;
&lt;span class="py"&gt;_.name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"currency_code"&lt;/span&gt;
&lt;span class="py"&gt;_.byte_size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="py"&gt;ISO3_AED&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;784&lt;/span&gt;
&lt;span class="py"&gt;ISO3_AFN&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;971&lt;/span&gt;
&lt;span class="py"&gt;ISO3_ALL&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="py"&gt;ISO3_AMD&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;51&lt;/span&gt;
&lt;span class="py"&gt;ISO3_ANG&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;532&lt;/span&gt;
&lt;span class="py"&gt;ISO3_AOA&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;973&lt;/span&gt;
&lt;span class="py"&gt;ISO3_ARS&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;
&lt;span class="py"&gt;ISO3_AUD&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Until C has reflection, extracting the enum label/value pairs from the debug section can provide an effective way to use this information in C programs. This gives you reflection-like capabilities in plain C — without changing your source code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No changes to existing enum definitions in the source code.&lt;/li&gt;
&lt;li&gt;No duplicate definitions.&lt;/li&gt;
&lt;li&gt;No runtime dependency on external tools or libraries.&lt;/li&gt;
&lt;li&gt;Always in sync with the compiled enum.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A compact package (~20KB) (one ".c" file, one ".h" file, and Python script to extract debug information from object files) is available here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/yairlenga/c-enum-reflect/releases/latest" rel="noopener noreferrer"&gt;https://github.com/yairlenga/c-enum-reflect/releases/latest&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See the &lt;a href="https://github.com/yairlenga/c-enum-reflect/releases/" rel="noopener noreferrer"&gt;Releases&lt;/a&gt; page for other versions and packages.&lt;/p&gt;

</description>
      <category>c</category>
      <category>programming</category>
      <category>coding</category>
      <category>systems</category>
    </item>
    <item>
      <title>Automatic Enum Stringification in C via Build-Time Code Generation</title>
      <dc:creator>Yair Lenga</dc:creator>
      <pubDate>Mon, 27 Apr 2026 11:00:00 +0000</pubDate>
      <link>https://forem.com/yairlenga/automatic-enum-stringification-in-c-via-build-time-code-generation-115k</link>
      <guid>https://forem.com/yairlenga/automatic-enum-stringification-in-c-via-build-time-code-generation-115k</guid>
      <description>&lt;p&gt;The C compilers (&lt;code&gt;gcc&lt;/code&gt;, &lt;code&gt;clang&lt;/code&gt;, ...) capture lot of information about the code and store in the information as debug information. Sometimes, we can leverage this information and enhance our code with minimal effort.&lt;/p&gt;

&lt;p&gt;I wrote about automatic &lt;strong&gt;&lt;code&gt;enum&lt;/code&gt; stringifcation&lt;/strong&gt; in C, using build-time code generation from DWARF debug info.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;No manual lookup tables to build or maintain, no complex macros - just compile, extract and link.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The final binary contains plain C data structures with zero runtime dependency on DWARF libraries, or tools.&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="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;country_code&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;ISO3_AFG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* Afghanistan */&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;ISO3_ALB&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="cm"&gt;/* Albania */&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;ISO3_ATA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="cm"&gt;/* Antarctica */&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;ISO3_DZA&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="cm"&gt;/* Algeria */&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="n"&gt;ENUM_DESCRIBE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;country3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;country_code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;country_code&lt;/span&gt; &lt;span class="n"&gt;c&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;"Called with C=%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;ENUM_LABEL_OF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;country3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&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;blockquote&gt;
&lt;p&gt;Medium Article (No Paywall): &lt;a href="https://www.reddit.com/r/C_Programming/comments/1stmivg/automatic_enum_stringification_in_c_via_buildtime/" rel="noopener noreferrer"&gt;Automatic Enum Stringification in C via Build-Time Code Generation&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;The process work in 4 steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The C code that uses the &lt;code&gt;enum&lt;/code&gt; is complied with debug mode (-g)&lt;/li&gt;
&lt;li&gt;A python program extract the enum definition from the object files, and generate the meta data that is needed to support the reflection as C code.&lt;/li&gt;
&lt;li&gt;The generated C code is compiled and linked into the final binary.&lt;/li&gt;
&lt;li&gt;At run time, the program read the meta data and and can complete the conversion with no additional dependencies.&lt;/li&gt;
&lt;/ol&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%2Fk867bp0ccfjlk9dk5dfz.png" 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%2Fk867bp0ccfjlk9dk5dfz.png" alt="Generating and using enum meta data" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Curious about what other techniques are used to solve similar problem.&lt;/p&gt;

</description>
      <category>c</category>
      <category>coding</category>
      <category>programming</category>
    </item>
    <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>
