memory-safety-patterns

Provide cross-language memory safety patterns for Rust, C++, and C.

Updated Jul 10, 2025
One-click install
npx skills add https://github.com/nubiv/my-nome --skill memory-safety-patterns-nubiv
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: memory-safety-patterns
Source: https://github.com/nubiv/my-nome/tree/main/nix-darwin/config/claude/skills/memory-safety-patterns
Command: npx skills add https://github.com/nubiv/my-nome --skill memory-safety-patterns-nubiv

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill addresses the critical challenge of memory safety in programming, helping developers prevent common bugs like use-after-free, memory leaks, and buffer overflows across multiple languages.

Core Features & Use Cases

  • Cross-Language Patterns: Learn and apply memory safety techniques in C++, Rust, and C.
  • RAII & Ownership: Understand resource management tied to object lifetimes and Rust's ownership model.
  • Smart Pointers: Utilize unique_ptr, shared_ptr, and weak_ptr for safe memory management in C++.
  • Bounds Checking: Implement safe array and vector access to prevent overflows.
  • Data Race Prevention: Learn techniques for thread-safe programming using atomics, mutexes, and RwLocks.
  • Use Case: When developing a high-performance systems application in C++ or Rust, ensure robust memory management and prevent common vulnerabilities by applying the patterns described.

Quick Start

Explain the RAII pattern in C++ with a code example.

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 use-after-free and memory leaks in C++?

To prevent use-after-free and memory leaks in C++, use RAII principles and smart pointers like unique_ptr and shared_ptr to tie resource management to object lifetimes, ensuring automatic deallocation and nullifying dangling pointers.

What is the best way to prevent data races in multi-threaded Rust applications?

Preventing data races in Rust requires using ownership rules and thread-safe constructs like atomics, mutexes, and RwLocks, which the language enforces at compile time to ensure safe concurrent memory access.

How does RAII help with memory safety across different programming languages?

RAII provides memory safety by binding resource lifecycles to object scope, automatically releasing memory and preventing leaks and double-free errors when objects go out of scope in languages like C++, Rust, and C.

When should I use weak_ptr instead of shared_ptr for memory management?

Use weak_ptr instead of shared_ptr to break circular reference cycles and prevent memory leaks, observing memory without claiming ownership, while shared_ptr is for shared ownership where the last owner deallocates the resource.

Can I implement bounds checking for buffer overflows in standard C?

Implementing bounds checking for buffer overflows in C requires manual validation of array and vector indices during access, as the language lacks built-in safety spectrums and automatic bounds enforcement found in higher-level constructs.

Rust ownership vs C++ smart pointers: which approach is better for resource management?

Rust ownership enforces memory safety at compile time via strict borrowing rules, while C++ smart pointers offer flexibility but rely on developer discipline; choose Rust for guaranteed safety or C++ for gradual integration into existing systems.