<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Struct on icyveins7's blog</title><link>https://icyveins7.github.io/tags/struct/</link><description>Recent content in Struct on icyveins7's blog</description><generator>Hugo</generator><language>en-us</language><copyright>This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.</copyright><lastBuildDate>Mon, 14 Jul 2025 20:00:00 +0800</lastBuildDate><atom:link href="https://icyveins7.github.io/tags/struct/index.xml" rel="self" type="application/rss+xml"/><item><title>Old dog, old C struct tricks</title><link>https://icyveins7.github.io/posts/2025/07/old-dog-old-c-struct-tricks/</link><pubDate>Mon, 14 Jul 2025 20:00:00 +0800</pubDate><guid>https://icyveins7.github.io/posts/2025/07/old-dog-old-c-struct-tricks/</guid><description>&lt;blockquote&gt;&#10;&lt;p&gt;Look at this code snippet and tell me with a straight face you didn&amp;rsquo;t think it was a memory leak at first either..&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;A while ago I worked on some code which contained a container that looked like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;struct&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;S&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; size_t sz;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; data[&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;];&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;void&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Set&lt;/span&gt;(size_t _sz, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; &lt;span style="color:#f92672"&gt;*&lt;/span&gt;_data){&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; sz &lt;span style="color:#f92672"&gt;=&lt;/span&gt; _sz;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; memcpy(data, _data, &lt;span style="color:#66d9ef"&gt;sizeof&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;)&lt;span style="color:#f92672"&gt;*&lt;/span&gt;sz);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;If you&amp;rsquo;re a modern programmer like me, you might be looking at this and thinking: &lt;em&gt;there&amp;rsquo;s no way this isn&amp;rsquo;t a flagrant memory violation in almost every case&lt;/em&gt;. Right? Well, it turns out this is something them old boys before C99 used to do.&lt;/p&gt;</description></item></channel></rss>