<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C on icyveins7's blog</title><link>https://icyveins7.github.io/tags/c/</link><description>Recent content in C 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/c/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><item><title>Gotta go (randomly) fast, thrust vs cuRAND</title><link>https://icyveins7.github.io/posts/2025/03/gotta-go-randomly-fast-thrust-vs-curand/</link><pubDate>Mon, 10 Mar 2025 20:00:00 +0800</pubDate><guid>https://icyveins7.github.io/posts/2025/03/gotta-go-randomly-fast-thrust-vs-curand/</guid><description>&lt;blockquote&gt;&#10;&lt;p&gt;How fast can you generate (pseudo-)random numbers on the GPU?&#10;How fast can you generate (pseudo-)random numbers on the GPU?&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;I recently had to answer this question. Well, not exactly this question, but this was a key part of it.&lt;/p&gt;&#10;&lt;p&gt;A quick google search will bring up two common methods (libraries) when trying to do this in CUDA: thrust and cuRAND. Thrust is known to be a lot easier to set up; no need to write the kernel code and nitty gritty details, so I started with that.&lt;/p&gt;</description></item><item><title>Cross-platform trigonometric SIMD and how the C ABI confused me</title><link>https://icyveins7.github.io/posts/2025/01/cross-platform-trigonometric-simd-and-how-the-c-abi-confused-me/</link><pubDate>Thu, 30 Jan 2025 20:00:00 +0800</pubDate><guid>https://icyveins7.github.io/posts/2025/01/cross-platform-trigonometric-simd-and-how-the-c-abi-confused-me/</guid><description>&lt;blockquote&gt;&#10;&lt;p&gt;My small journey in discovering libmvec functions and the C problems I encountered..&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;I was recently working on making an existing Windows-only library of SIMD functions cross-platform i.e. making everything work in Linux. Here I&amp;rsquo;m going to highlight some problems I encountered in the process; hopefully it will help someone if they encounter something similar too.&lt;/p&gt;&#10;&lt;h1 id="the-windows-msvc-function"&gt;The Windows (MSVC) function&lt;/h1&gt;&#10;&lt;p&gt;Intrinsics are usually tied to compiler implementations. The one that gave me issues was a bunch of trigonometric functions. Let&amp;rsquo;s use the SSE version that works on floats; in MSVC this was implemented as a simple&lt;/p&gt;</description></item></channel></rss>