memory-safety-patterns

Implement memory-safe code using RAII, ownership, and smart pointers in Rust, C++, and C.

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill memory-safety-patterns-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: memory-safety-patterns
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/memory-safety-patterns
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill memory-safety-patterns-sanketadlak

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Systems programmers frequently introduce memory bugs such as use-after-free, double-free, leaks, buffer overflows, and data races when managing resources manually. This Skill provides concrete, cross-language patterns that prevent these bugs at the source. ## Core Features & Use Cases - RAII and Smart Pointers in C++: Tie resource lifetimes to object scope with unique_ptr, shared_ptr, weak_ptr, lock guards, and custom deleters. - Ownership and Borrowing in Rust: Apply move semantics, lifetimes, interior mutability, Rc, and Arc to enforce safety at compile time. - Safe Resource Management in C: Use goto-cleanup, opaque create/destroy pairs, and cleanup attributes to avoid leaks without language support. - Use Case: When writing a multithreaded C++ service that shares a cache across threads, apply the shared_mutex reader-writer pattern and atomic counters shown here to eliminate data races before they reach production. ## Quick Start Ask the AI to show you how to manage a file handle and a shared cache safely in C++ using RAII and smart pointers.

Frequently Asked Questions about memory-safety-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent memory leaks in C++?

Use RAII to tie resource lifetimes to object scope and prefer smart pointers like unique_ptr and shared_ptr over raw new and delete. Containers such as std::vector and std::fstream release their resources automatically in destructors.

How does Rust ownership prevent use-after-free?

Rust's move semantics transfer ownership so the old binding becomes invalid, and the borrow checker rejects references that outlive their data. Lifetime annotations let the compiler verify reference validity at compile time.

When should I use shared_ptr vs unique_ptr in C++?

Use unique_ptr for single ownership, which is the default and cheapest option. Use shared_ptr only when multiple owners genuinely share a resource, and pair it with weak_ptr to break reference cycles.

How do I manage resources safely in C without RAII?

Use the goto-cleanup pattern so every allocation has a single cleanup path, or wrap resources in opaque create/destroy function pairs. GCC and Clang also support a cleanup attribute that frees variables at scope exit.

What tools detect memory bugs in C and C++?

AddressSanitizer catches use-after-free and overflows at runtime, Valgrind detects leaks and invalid accesses, and ThreadSanitizer finds data races. For Rust, Miri detects undefined behavior in unsafe code.

How do I prevent data races in multithreaded Rust code?

Rust prevents data races at compile time through the Send and Sync traits. Use Arc with Mutex or RwLock for shared mutable state across threads, and atomic types like AtomicI32 for simple counters.