Skip to content
Skillpedia
General coding41k · #6799

memory-safety-patterns

What it does

Helps write computer code that manages memory safely, avoiding common errors like leaks or using memory that has already been freed. This is done using techniques like RAII, smart pointers, and ownership.

When to use it

  • ✓When creating programs that need to be very reliable (operating systems, games).
  • ✓When you want to prevent your program from crashing or behaving strangely due to memory errors.
  • ✓When learning or using languages like C++, Rust, or C to ensure safety.

Skill link

https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/memory-safety-patterns/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/memory-safety-patterns/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute →

Example of what to ask your AI

“How can I prevent memory leaks in C++ using RAII?”

5 new skills a week. 1 minute to read.

How it works

This tool teaches programming patterns that tie the lifetime of resources (like memory or files) to the lifetime of objects. When an object is created, it acquires a resource; when the object is destroyed, it automatically releases the resource.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

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
FAQ

What is RAII?

RAII stands for 'Resource Acquisition Is Initialization'. It's a technique where acquiring a resource (like allocating memory) happens during object initialization, and releasing it happens when the object is destroyed.

What are smart pointers?

These are objects that act like pointers but automatically manage the memory they point to, helping to prevent errors like leaks or accessing invalid memory.

What's the difference between C and Rust regarding memory safety?

C requires the programmer to manage memory manually, which is error-prone. Rust uses an 'ownership' system that guarantees memory safety at compile time, without needing a garbage collector.

Details

Original description by the author

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.

repo: wshobson/agents

Popularity

GitHub stars
41k
Ranking position
#6799
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills