memory-safety-patterns

Implement memory-safe programming with RAII, ownership, and smart pointers across Rust, C++, and C.

Updated Apr 15, 2025
One-click install
npx skills add https://github.com/khrore/nix-config --skill memory-safety-patterns-khrore
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: memory-safety-patterns
Source: https://github.com/khrore/nix-config/tree/main/dotfiles/common/.config/opencode/skills/memory-safety-patterns
Command: npx skills add https://github.com/khrore/nix-config --skill memory-safety-patterns-khrore

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

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.

Core Features & Use Cases

  • RAII: ensure deterministic resource cleanup via object lifetime
  • Ownership & smart pointers: prevent leaks and double frees
  • Cross-language patterns: apply memory-safety strategies in Rust, C++, and C
  • Concurrency safety: use locks and atomics to avoid data races

Quick Start

Apply RAII and ownership models in a small Rust project to manage a file handle from open to close.

Frequently Asked Questions about memory-safety-patterns

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

FAQPage Schema
What is RAII and how does it help with memory safety in C++?

RAII (Resource Acquisition Is Initialization) ties resource lifetimes to object scope, ensuring deterministic cleanup. It prevents leaks and double frees by automatically releasing memory handles, file locks, and mutexes when objects go out of scope in C++ systems programming.

How do I implement ownership and smart pointers to manage resources across Rust and C++?

Ownership and smart pointers manage resource lifecycles by enforcing safe transfer rules. Use Rust's ownership model or C++ smart pointers to control resource allocation, prevent double frees, and guarantee cleanup without manual memory intervention.

Does this approach work for preventing data races in concurrent systems programming?

Data race prevention relies on concurrency safeguards like locks and atomics. Apply language-appropriate patterns in Rust and C++ to synchronize access, ensuring memory safety across threads when managing shared resources or mutexes.

What's the best way to handle memory safety when writing systems code in C without smart pointers?

Memory safety in C requires manual resource lifecycle control since it lacks built-in smart pointers. Apply ownership patterns and strict allocation discipline to manage pointers and file handles, preventing leaks and undefined behavior in systems code.

When do I need to use smart pointers instead of raw pointers for resource management?

Smart pointers are needed when managing dynamic resource lifecycles to prevent leaks and double frees. Use them over raw pointers when safe ownership transfer and deterministic cleanup are required, especially in complex C++ systems programming.

Why does transferring ownership in Rust prevent memory bugs like double frees?

Transferring ownership in Rust prevents memory bugs by enforcing strict borrowing rules at compile time. The compiler tracks resource lifecycles, guaranteeing single ownership and eliminating double frees, leaks, and data races automatically.