๐ค
C++
Avoid common C++ mistakes โ memory leaks, dangling references, undefined behavior, and ownership confusion.
ๅฎๅ
จ้่ฟ
๐ฌPrompt
ๆ่ฝ่ฏดๆ
name: C++ slug: cpp version: 1.0.1 description: Write safe C++ avoiding memory leaks, dangling pointers, undefined behavior, and ownership confusion. metadata: {"clawdbot":{"emoji":"โก","requires":{"bins":["g++"]},"os":["linux","darwin","win32"]}}
Quick Reference
| Topic | File |
|---|---|
| RAII, smart pointers, new/delete | memory.md |
| Raw pointers, references, nullptr | pointers.md |
| Rule of 3/5/0, inheritance, virtual | classes.md |
| Containers, iterators, algorithms | stl.md |
| Templates, SFINAE, concepts | templates.md |
| Threads, mutex, atomics | concurrency.md |
| C++11/14/17/20, move semantics | modern.md |
| Undefined behavior traps | ub.md |
Critical Rules
- Raw
newwithoutdeleteleaks โ usestd::unique_ptrorstd::make_unique - Returning reference to local โ undefined behavior, object destroyed on return
==for C-strings compares pointers โ usestd::stringorstrcmp()- Signed integer overflow is UB โ not wrap-around like unsigned
- Virtual destructor required in base class โ otherwise derived destructor skipped
std::movedoesn't move โ it casts to rvalue, enabling move semantics- Moved-from object valid but unspecified โ don't use without reassigning
- Data race on non-atomic is UB โ use
std::mutexorstd::atomic vector<bool>is not a real container โ returns proxy, usedeque<bool>map[key]inserts default if missing โ usefind()orcontains()to check- Braced init
{}prevents narrowing โint x{3.5}errors,int x(3.5)truncates - Iterator invalidation on
push_backโ vector may relocate, invalidating iterators string_viewdoesn't own data โ underlying string must outlive the view
ๅฆไฝไฝฟ็จใC++ใ๏ผ
- ๆๅผๅฐ้พ่พAI๏ผWeb ๆ iOS App๏ผ
- ็นๅปไธๆนใ็ซๅณไฝฟ็จใๆ้ฎ๏ผๆๅจๅฏน่ฏๆกไธญ่พๅ ฅไปปๅกๆ่ฟฐ
- ๅฐ้พ่พAI ไผ่ชๅจๅน้ ๅนถ่ฐ็จใC++ใๆ่ฝๅฎๆไปปๅก
- ็ปๆๅณๆถๅ็ฐ๏ผๆฏๆ็ปง็ปญๅฏน่ฏไผๅ