Claude Cursor Skill

memory-safety-patterns

Implement memory-safe programming with RAII, ownership, smart pointers, and resource management across Rust, C++, and C. Use when writing safe systems code, managing resources, or preventing memory bugs.

LLM Mart · 0 points · 15 views 0 listing impressions 0 install-command copies
Download wshobson-agents-plugins_systems-programming_skills_memory-safety-patterns-554237f.zip · 5 KB

Install

skills CLI npx skills add https://github.com/wshobson/agents/tree/main/plugins/systems-programming/skills/memory-safety-patterns
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install wshobson-agents@llmmart
Git git clone https://github.com/wshobson/agents.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole wshobson/agents collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

Memory Safety Patterns

Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.

When to Use This Skill

  • Writing memory-safe systems code
  • Managing resources (files, sockets, memory)
  • Preventing use-after-free and leaks
  • Implementing RAII patterns
  • Choosing between languages for safety
  • Debugging memory issues

Core Concepts

1. Memory Bug Categories

Bug Type Description Prevention
Use-after-free Access freed memory Ownership, RAII
Double-free Free same memory twice Smart pointers
Memory leak Never free memory RAII, GC
Buffer overflow Write past buffer end Bounds checking
Dangling pointer Pointer to freed memory Lifetime tracking
Data race Concurrent unsynchronized access Ownership, Sync

2. Safety Spectrum

Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)
Less safe                                              More safe
More control                                           Less control

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices

Do's

  • Prefer RAII - Tie resource lifetime to scope
  • Use smart pointers - Avoid raw pointers in C++
  • Understand ownership - Know who owns what
  • Check bounds - Use safe access methods
  • Use tools - AddressSanitizer, Valgrind, Miri

Don'ts

  • Don't use raw pointers - Unless interfacing with C
  • Don't return local references - Dangling pointer
  • Don't ignore compiler warnings - They catch bugs
  • Don't use unsafe carelessly - In Rust, minimize it
  • Don't assume thread safety - Be explicit

Debugging Tools

# AddressSanitizer (Clang/GCC)
clang++ -fsanitize=address -g source.cpp

# Valgrind
valgrind --leak-check=full ./program

# Rust Miri (undefined behavior detector)
cargo +nightly miri run

# ThreadSanitizer
clang++ -fsanitize=thread -g source.cpp
Files (agents)
  • references
    • details.md 10.8 KB
      Loading…
  • SKILL.md 2.5 KB
    ---
    name: memory-safety-patterns
    description: Implement memory-safe programming with RAII, ownership, smart pointers, and resource management across Rust, C++, and C. Use when writing safe systems code, managing resources, or preventing memory bugs.
    ---
    
    # Memory Safety Patterns
    
    Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.
    
    ## When to Use This Skill
    
    - Writing memory-safe systems code
    - Managing resources (files, sockets, memory)
    - Preventing use-after-free and leaks
    - Implementing RAII patterns
    - Choosing between languages for safety
    - Debugging memory issues
    
    ## Core Concepts
    
    ### 1. Memory Bug Categories
    
    | Bug Type             | Description                      | Prevention        |
    | -------------------- | -------------------------------- | ----------------- |
    | **Use-after-free**   | Access freed memory              | Ownership, RAII   |
    | **Double-free**      | Free same memory twice           | Smart pointers    |
    | **Memory leak**      | Never free memory                | RAII, GC          |
    | **Buffer overflow**  | Write past buffer end            | Bounds checking   |
    | **Dangling pointer** | Pointer to freed memory          | Lifetime tracking |
    | **Data race**        | Concurrent unsynchronized access | Ownership, Sync   |
    
    ### 2. Safety Spectrum
    
    ```
    Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)
    Less safe                                              More safe
    More control                                           Less control
    ```
    
    ## Detailed patterns and worked examples
    
    Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
    
    ## Best Practices
    
    ### Do's
    
    - **Prefer RAII** - Tie resource lifetime to scope
    - **Use smart pointers** - Avoid raw pointers in C++
    - **Understand ownership** - Know who owns what
    - **Check bounds** - Use safe access methods
    - **Use tools** - AddressSanitizer, Valgrind, Miri
    
    ### Don'ts
    
    - **Don't use raw pointers** - Unless interfacing with C
    - **Don't return local references** - Dangling pointer
    - **Don't ignore compiler warnings** - They catch bugs
    - **Don't use `unsafe` carelessly** - In Rust, minimize it
    - **Don't assume thread safety** - Be explicit
    
    ## Debugging Tools
    
    ```bash
    # AddressSanitizer (Clang/GCC)
    clang++ -fsanitize=address -g source.cpp
    
    # Valgrind
    valgrind --leak-check=full ./program
    
    # Rust Miri (undefined behavior detector)
    cargo +nightly miri run
    
    # ThreadSanitizer
    clang++ -fsanitize=thread -g source.cpp
    ```
    

Comments (0)

Sign in to join the conversation.

No comments yet.

Reviews (0)

No reviews yet.