<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[💡 Memory, pointers and volatile]]></title><description><![CDATA[<p dir="auto">Desktop C forgives you. Firmware C doesn't. The same program that runs fine on a laptop can corrupt memory or miss an interrupt on a microcontroller — because of what the compiler and the hardware actually do with your code.</p>
<h3>The bugs that keep showing up here</h3>
<p dir="auto"><strong>The missing <code>volatile</code>.</strong> A flag shared between main code and an interrupt handler works perfectly at -O0 and breaks at -O2, because the compiler cached it in a register. It never re-read memory — you never told it the value could change behind its back. The canonical shape looks like this:</p>
<pre><code class="language-c">volatile bool data_ready = false;  // set in ISR, polled in main loop

void ADC_ISR(void) {
    data_ready = true;
}

void loop(void) {
    if (data_ready) {              // re-read from memory every time
        data_ready = false;
        process_sample();
    }
}
</code></pre>
<p dir="auto">Drop the <code>volatile</code> and the compiler hoists the read out of the loop — the loop never sees the interrupt fire. Diagnosing it: if it works at -O0 but breaks at -O2, suspect a missing qualifier before suspecting hardware. Any variable touched both inside and outside an ISR gets <code>volatile</code>. No exceptions, no "but it works on my machine".<br />
→ <a href="https://embiq.grapesiot.com/foundation/02-embedded-c-programming/05-interrupts-and-isr-patterns" rel="nofollow ugc">Interrupts and ISR patterns</a></p>
<p dir="auto"><strong>Structs that don't match the hardware.</strong> You overlay a struct on a peripheral register map, but the compiler inserts padding where you didn't expect it, and now every field after the gap writes to the wrong address. On desktop this is a curiosity; on hardware it configures a peripheral into nonsense. Learn struct layout and padding, then verify with <code>sizeof</code> and <code>offsetof</code> instead of trusting the layout.<br />
→ <a href="https://embiq.grapesiot.com/foundation/02-embedded-c-programming/02-memory-pointers-data-representation" rel="nofollow ugc">Memory, pointers and data representation</a></p>
<p dir="auto"><strong>Read-modify-write that isn't atomic.</strong> <code>GPIO_REG |= (1 &lt;&lt; PIN)</code> looks harmless until an interrupt fires between the read and the write and modifies a neighbouring pin — your write-back silently undoes it. On real firmware this shows up as a pin that "randomly" changes state once a week. Critical regions and atomic access exist for exactly this reason. Integer promotion adds a second trap: your <code>uint8_t</code> arithmetic quietly becomes <code>int</code> mid-expression, signed-vs-unsigned comparisons go sideways, and shifting into the sign bit is undefined behaviour the compiler is allowed to punish.<br />
→ <a href="https://embiq.grapesiot.com/foundation/02-embedded-c-programming/06-writing-robust-embedded-c" rel="nofollow ugc">Writing robust embedded C</a></p>
<hr />
<p dir="auto"><em>Start at <a href="https://embiq.grapesiot.com/foundation/02-embedded-c-programming/" rel="nofollow ugc">the Embedded C track</a> and work through it in order. Post the smallest reproducer plus your compiler flags and optimisation level. Tag with <code>c-lang</code>.</em></p>
]]></description><link>https://forum.grapesiot.com/topic/70/memory-pointers-and-volatile</link><generator>RSS for Node</generator><lastBuildDate>Thu, 19 Nov 2026 13:02:49 GMT</lastBuildDate><atom:link href="https://forum.grapesiot.com/topic/70.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 03 Oct 2026 11:29:36 GMT</pubDate><ttl>60</ttl></channel></rss>