Skills · Coding

Memory Safety Patterns

Unverified30/40

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.

Originally by wshobson · MIT

Claude CodePartialHas SKILL.md but declares no allowed-tools — Claude Code will ask for permission each time
Cursor·UnknownWe have not crawled the repo tree, so we will not guess
Codex·UnknownWe have not crawled the repo tree, so we will not guess
Gemini CLI·UnknownThe spec defines no detection rule for Gemini
Copilot·UnknownWe have not crawled the repo tree, so we will not guess
npx agentalley add memory-safety-patterns

This command does not work yet — the CLI is still being built. Until then, use Raw in the reader below to take the file.

Who is stuck, and on what

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.

The whole source

No sign-in, no blur, nothing truncated
memory-safety-patterns/SKILL.md77 lines2.5 KBRawView on GitHub
Frontmatter — 2 properties
namememory-safety-patterns
descriptionImplement 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.
1---
2name: memory-safety-patterns
3description: 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.
4---A5No allowed-tools declared — no way to tell what this skill may touch
5 
6# Memory Safety Patterns
7 
8Cross-language patterns for memory-safe programming including RAII, ownership, smart pointers, and resource management.
9 
10## When to Use This Skill
11 
12- Writing memory-safe systems code
13- Managing resources (files, sockets, memory)
14- Preventing use-after-free and leaks
15- Implementing RAII patterns
16- Choosing between languages for safety
17- Debugging memory issues
18 
19## Core Concepts
20 
21### 1. Memory Bug Categories
22 
23| Bug Type | Description | Prevention |
24| -------------------- | -------------------------------- | ----------------- |
25| **Use-after-free** | Access freed memory | Ownership, RAII |
26| **Double-free** | Free same memory twice | Smart pointers |
27| **Memory leak** | Never free memory | RAII, GC |
28| **Buffer overflow** | Write past buffer end | Bounds checking |
29| **Dangling pointer** | Pointer to freed memory | Lifetime tracking |
30| **Data race** | Concurrent unsynchronized access | Ownership, Sync |
31 
32### 2. Safety Spectrum
33 
34```
35Manual (C) → Smart Pointers (C++) → Ownership (Rust) → GC (Go, Java)
36Less safe More safe
37More control Less control
38```
39 
40## Detailed patterns and worked examples
41 
42Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
43 
44## Best Practices
45 
46### Do's
47 
48- **Prefer RAII** - Tie resource lifetime to scope
49- **Use smart pointers** - Avoid raw pointers in C++
50- **Understand ownership** - Know who owns what
51- **Check bounds** - Use safe access methods
52- **Use tools** - AddressSanitizer, Valgrind, Miri
53 
54### Don'ts
55 
56- **Don't use raw pointers** - Unless interfacing with C
57- **Don't return local references** - Dangling pointer
58- **Don't ignore compiler warnings** - They catch bugs
59- **Don't use `unsafe` carelessly** - In Rust, minimize it
60- **Don't assume thread safety** - Be explicit
61 
62## Debugging Tools
63 
64```bash
65# AddressSanitizer (Clang/GCC)
66clang++ -fsanitize=address -g source.cpp
67 
68# Valgrind
69valgrind --leak-check=full ./program
70 
71# Rust Miri (undefined behavior detector)
72cargo +nightly miri run
73 
74# ThreadSanitizer
75clang++ -fsanitize=thread -g source.cpp
76```
77 

Reviews

Installed this one?Write the first review and take the Trailblazer badge.

Reviews only open after a real install, so this is empty — and we leave it empty rather than invent one.

Alternatives

Also in Coding