Best practices for C++ Memory Management Modern
.cursorrules in your project root# C++ Memory Management Modern ===================================== ## Core Principles ------------------- 1. **Use Smart Pointers**: Prefer smart pointers (`std::unique_ptr`, `std::shared_ptr`) over raw pointers to manage dynamic memory. 2. **Avoid Manual Memory Management**: Minimize the use of `new` and `delete` operators. Instead, rely on smart pointers and containers to manage memory. 3. **Container-Based Memory Management**: Use standard library containers (e.g., `std::vector`, `std::array`) to manage collections of objects. 4. **RAII (Resource Acquisition Is Initialization)**: Ensure that resources, including memory, are acquired and released using RAII idiom. ## Code Style Guidelines ------------------------ ### Naming 1. **Prefix Smart Pointers**: Use `p_` or `ptr_` prefix for smart pointer member variables. 2. **Suffix Raw Pointers**: Use `_raw` suffix for raw pointer variables. ### Typing 1. **Use `std::unique_ptr` for Exclusive Ownership**: Use `std::unique_ptr` when a single owner is responsible for the memory. 2. **Use `std::shared_ptr` for Shared Ownership**: Use `std::shared_ptr` when multiple owners share responsibility for the memory. 3. **Use `std::weak_ptr` for Observers**: Use `std::weak_ptr` for observers that do not own the memory. ## Best Practices ------------------ 1. **Use `std::make_unique` and `std::make_shared`**: Prefer `std::make_unique` and `std::make_shared` over constructors for creating smart pointers. 2. **Avoid Naked `new`**: Never use `new` without immediately assigning the result to a smart pointer. 3. **Use `std::vector` for Dynamic Arrays**: Use `std::vector` instead of manual memory management for dynamic arrays. 4. **Prefer `std::array` for Fixed-Size Arrays**: Use `std::array` for fixed-size arrays instead of C-style arrays. 5. **Use `std::move` for Efficient Transfer**: Use `std::move` to transfer ownership of objects instead of copying. ## Common Pitfalls to Avoid --------------------------- 1. **Dangling Pointers**: Avoid returning or storing pointers to local variables or temporary objects. 2. **Double Deletion**: Never delete an object that is already managed by a smart pointer. 3. **Circular References**: Avoid creating circular references between `std::shared_ptr` objects. 4. **Raw Pointer Arithmetic**: Avoid using pointer arithmetic with raw pointers; instead, use iterators or smart pointers. 5. **Memory Leaks**: Use tools like Valgrind or AddressSanitizer to detect memory leaks.
Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install C++ Memory Management Modern.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.