memory-safety-patterns
它能做什么
帮助你编写能安全管理内存的代码,避免内存泄漏、使用已释放内存等常见错误。这主要通过 RAII、智能指针和所有权等技术来实现。
适用场景
- ✓开发对可靠性要求很高的程序时(比如操作系统、游戏)。
- ✓想避免程序因内存错误而崩溃或行为异常时。
- ✓学习或使用 C++、Rust、C 等语言,需要确保内存安全时。
技能链接
https://github.com/wshobson/agents/blob/main/plugins/systems-programming/skills/memory-safety-patterns/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“如何在 C++ 中使用 RAII 防止内存泄漏?”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具会教你一些编程模式,把资源(比如内存或文件)的生命周期与对象的生命周期绑定在一起。对象被创建时获取资源;对象被销毁时,资源会自动释放。
技术内容(原始 SKILL.md)
作者在 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
unsafecarelessly - 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
常见问题
什么是 RAII?
RAII 全称是 Resource Acquisition Is Initialization,即“资源获取即初始化”。它的意思是:在对象初始化时获取资源(比如分配内存),在对象销毁时自动释放资源。
什么是智能指针?
智能指针的用法和普通指针类似,但会自动管理它所指向的内存,有助于避免内存泄漏或访问无效内存等错误。
C 和 Rust 在内存安全方面有什么区别?
C 需要程序员手动管理内存,容易出错。Rust 则通过“所有权”系统,在编译阶段就保证内存安全,并且不需要垃圾回收器。
详情
作者的原始描述
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
热度
- GitHub 星标
- 41k
- 排名
- #6799
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …