Write crash-free, secure code using proven memory safety patterns across Rust, C++, and C. Prevent leaks, use-after-free, and buffer overflows with RAII, ownership models, and smart pointers.
Die Skill-ZIP herunterladen
In Claude hochladen
Gehe zu Einstellungen → Fähigkeiten → Skills → Skill hochladen
Einschalten und loslegen
Teste es
Verwendung von "memory-safety-patterns". How do I prevent memory leaks when opening files in C++?
Erwartetes Ergebnis:
Use RAII with a smart file wrapper class or std::fstream. The file automatically closes when the object is destroyed, even if an exception occurs. Example: std::fstream file("data.txt"); - destructor handles cleanup automatically.
Verwendung von "memory-safety-patterns". What is the difference between Box, Rc, and Arc in Rust?
Erwartetes Ergebnis:
Box<T> for single ownership with heap allocation. Rc<T> for shared ownership (single-threaded) - increments reference count on clone. Arc<T> for atomic shared ownership across threads - thread-safe reference counting.
Verwendung von "memory-safety-patterns". How do I handle bounds checking safely?
Erwartetes Ergebnis:
In C++, use std::vector::at() which throws std::out_of_range, or std::span for array views. In Rust, indexing with [] panics on out-of-bounds, while .get() returns Option for safe handling. Prefer iterators to avoid manual bounds checking.
Sicherheitsaudit
SicherAll 56 static findings are false positives. The skill contains only documentation and code examples for memory safety patterns. Thread spawning (thread::spawn) was misidentified as process spawning. Markdown backticks for code formatting were misidentified as shell execution. Documentation URLs and debugging tool references are legitimate educational content.
Qualitätsbewertung
Was du bauen kannst
Systems Programming
Write operating systems, drivers, or embedded software that is free from memory corruption vulnerabilities.
Security-Critical Applications
Build applications where memory safety is mandatory: cryptography, network services, file processing.
Legacy Code Modernization
Apply memory safety patterns when refactoring C/C++ codebases to reduce bugs and improve reliability.
Probiere diese Prompts
Show me how to implement a RAII pattern in C++ for a file handle class. Include proper copy/move semantics and ensure the file is automatically closed when the object goes out of scope.
Explain how Rust ownership prevents use-after-free bugs. Show an example with a struct that holds a reference, including lifetime annotations and when to use Box, Rc, and Arc.
Write a C function that opens a file, allocates memory, and processes data. Use the goto-cleanup pattern to ensure all resources are properly freed on error.
Implement a thread-safe counter in both C++ using atomics and mutex, and Rust using Arc and Mutex. Show how each language prevents data races.
Bewährte Verfahren
- Prefer RAII and smart pointers over manual memory management to ensure automatic cleanup
- Use the borrow checker in Rust rather than fighting it - it prevents real bugs
- Run AddressSanitizer and Valgrind during development to catch memory issues early
Vermeiden
- Using raw pointers in C++ instead of smart pointers - leads to leaks and dangling pointers
- Ignoring compiler warnings about dangling references - they indicate real bugs
- Using unsafe blocks in Rust without documenting why and isolating them from safe code
Häufig gestellte Fragen
What is RAII and why does it matter?
How does Rust prevent memory bugs?
When should I use smart pointers in C++?
What is the goto-cleanup pattern in C?
How do I choose between Rust and C++ for memory safety?
What debugging tools help catch memory issues?
Entwicklerdetails
Autor
sickn33Lizenz
MIT
Repository
https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/memory-safety-patternsRef
main
Dateistruktur