Memory Safety Patterns

by wshobson46891e7e60daNo licenseListed Oct 8, 2026Updated Oct 8, 2026

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.

Instructions onlySoftware Development
AI-generated overview

Cross-language guidance on memory-safe programming patterns in Rust, C++ and C.

What it does
This skill provides reference guidance on memory-safe programming across Rust, C++ and C. It covers memory bug categories such as use-after-free, double-free, leaks and buffer overflows, the safety spectrum from manual memory management to garbage collection, and best practices like RAII, smart pointers and ownership. It also lists debugging tools such as AddressSanitizer, Valgrind, ThreadSanitizer and Rust Miri, with further patterns in a bundled reference file.
When to use it
Use it when writing systems code in Rust, C++ or C, managing resources such as files, sockets or memory, or preventing use-after-free, leaks and dangling pointers. It also fits choosing between languages for safety and debugging memory issues.
Requirements
No scripts; instructions and a reference document only. Following the debugging guidance assumes access to toolchains and tools such as Clang or GCC, Valgrind, and a nightly Rust toolchain with Miri.

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 TypeDescriptionPrevention
Use-after-freeAccess freed memoryOwnership, RAII
Double-freeFree same memory twiceSmart pointers
Memory leakNever free memoryRAII, GC
Buffer overflowWrite past buffer endBounds checking
Dangling pointerPointer to freed memoryLifetime tracking
Data raceConcurrent unsynchronized accessOwnership, Sync

2. Safety Spectrum

Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)Less safe                                              More safeMore 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
# Valgrindvalgrind --leak-check=full ./program
# Rust Miri (undefined behavior detector)cargo +nightly miri run
# ThreadSanitizerclang++ -fsanitize=thread -g source.cpp

Source and attribution

Source:wshobson/agentsinplugins/systems-programming/skills/memory-safety-patternsat commit46891e7

License: No license

Content belongs to its original authors. SourceWeft indexes it from a public repository.

Report or request removal