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.
تنزيل ZIP المهارة
رفع في Claude
اذهب إلى Settings → Capabilities → Skills → Upload skill
فعّل وابدأ الاستخدام
اختبرها
استخدام "memory-safety-patterns". How do I prevent memory leaks when opening files in C++?
النتيجة المتوقعة:
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.
استخدام "memory-safety-patterns". What is the difference between Box, Rc, and Arc in Rust?
النتيجة المتوقعة:
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.
استخدام "memory-safety-patterns". How do I handle bounds checking safely?
النتيجة المتوقعة:
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.
التدقيق الأمني
آمنAll 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.
درجة الجودة
ماذا يمكنك بناءه
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.
جرّب هذه الموجهات
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.
أفضل الممارسات
- 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
تجنب
- 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
الأسئلة المتكررة
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?
تفاصيل المطور
المؤلف
sickn33الترخيص
MIT
المستودع
https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/memory-safety-patternsمرجع
main
بنية الملفات