Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Embiq Community

  1. Home
  2. Knowledge Base
  3. C Programming Tips & Tricks
  4. šŸ’” Memory, pointers and volatile

šŸ’” Memory, pointers and volatile

Scheduled Pinned Locked Moved C Programming Tips & Tricks
c-langembedded-cfirmware
1 Posts 1 Posters 84 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    admin
    wrote on last edited by
    #1

    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.

    The bugs that keep showing up here

    The missing volatile. 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:

    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();
        }
    }
    

    Drop the volatile 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 volatile. No exceptions, no "but it works on my machine".
    → Interrupts and ISR patterns

    Structs that don't match the hardware. 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 sizeof and offsetof instead of trusting the layout.
    → Memory, pointers and data representation

    Read-modify-write that isn't atomic. GPIO_REG |= (1 << PIN) 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 uint8_t arithmetic quietly becomes int mid-expression, signed-vs-unsigned comparisons go sideways, and shifting into the sign bit is undefined behaviour the compiler is allowed to punish.
    → Writing robust embedded C


    Start at the Embedded C track and work through it in order. Post the smallest reproducer plus your compiler flags and optimisation level. Tag with c-lang.

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better šŸ’—

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    Powered by NodeBB Contributors
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups