# C Cpp Patterns

> C/C++ Embedded Patterns: Memory and RAII

- Skill: `j4flmao/c-cpp-patterns` (Agent Skill)
- Install (CLI): `npx skillmds@latest add j4flmao/c-cpp-patterns`
- Raw SKILL.md: https://api.skillmd.com/api/skills/j4flmao/c-cpp-patterns/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: j4flmao (https://skillmd.com/u/j4flmao)
- Updated: 2026-09-21
- Page: https://skillmd.com/skills/j4flmao/c-cpp-patterns

---

# C/C++ Embedded Patterns: Memory and RAII

In embedded C/C++, precise memory layout control is critical. Memory alignment ensures that variables reside at addresses that are multiples of their size, preventing unaligned access faults on architectures like ARM Cortex-M. The compiler introduces padding bytes in structs to satisfy these constraints.

RAII (Resource Acquisition Is Initialization) ties resource management to object lifetime. When an object is instantiated, its constructor acquires the resource (e.g., a hardware mutex). When the object goes out of scope, its destructor is automatically called, releasing the resource. Pointer arithmetic allows direct manipulation of memory addresses, essential for interacting with memory-mapped peripheral registers.

```mermaid
%%{init: {"theme": "default", "flowchart": {"useMaxWidth": true}}}%%
flowchart TD
    subgraph ScopeFunctionScope ["Function Scope<br><br><br>"]
        A[Enter Scope] -->|"Construct()"| B[RAII Object]
        B -->|"AcquireMutex()"| C[Hardware Resource]
    end
    subgraph ExitScopeExit ["Scope Exit<br><br><br>"]
        B -->|"Destruct()"| D[Release Mutex]
    end
```

