๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
ๅฐ้พ™่™พๅฐ้พ™่™พAI
๐Ÿค–

C++

Avoid common C++ mistakes โ€” memory leaks, dangling references, undefined behavior, and ownership confusion.

ไธ‹่ฝฝ2.1k
ๆ˜Ÿๆ ‡2
็‰ˆๆœฌ1.0.1
AI ๆ™บ่ƒฝไฝ“
ๅฎ‰ๅ…จ้€š่ฟ‡
๐Ÿ’ฌ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

TopicFile
RAII, smart pointers, new/deletememory.md
Raw pointers, references, nullptrpointers.md
Rule of 3/5/0, inheritance, virtualclasses.md
Containers, iterators, algorithmsstl.md
Templates, SFINAE, conceptstemplates.md
Threads, mutex, atomicsconcurrency.md
C++11/14/17/20, move semanticsmodern.md
Undefined behavior trapsub.md

Critical Rules

  • Raw new without delete leaks โ€” use std::unique_ptr or std::make_unique
  • Returning reference to local โ€” undefined behavior, object destroyed on return
  • == for C-strings compares pointers โ€” use std::string or strcmp()
  • Signed integer overflow is UB โ€” not wrap-around like unsigned
  • Virtual destructor required in base class โ€” otherwise derived destructor skipped
  • std::move doesn'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::mutex or std::atomic
  • vector<bool> is not a real container โ€” returns proxy, use deque<bool>
  • map[key] inserts default if missing โ€” use find() or contains() 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_view doesn't own data โ€” underlying string must outlive the view

ๅฆ‚ไฝ•ไฝฟ็”จใ€ŒC++ใ€๏ผŸ

  1. ๆ‰“ๅผ€ๅฐ้พ™่™พAI๏ผˆWeb ๆˆ– iOS App๏ผ‰
  2. ็‚นๅ‡ปไธŠๆ–นใ€Œ็ซ‹ๅณไฝฟ็”จใ€ๆŒ‰้’ฎ๏ผŒๆˆ–ๅœจๅฏน่ฏๆก†ไธญ่พ“ๅ…ฅไปปๅŠกๆ่ฟฐ
  3. ๅฐ้พ™่™พAI ไผš่‡ชๅŠจๅŒน้…ๅนถ่ฐƒ็”จใ€ŒC++ใ€ๆŠ€่ƒฝๅฎŒๆˆไปปๅŠก
  4. ็ป“ๆžœๅณๆ—ถๅ‘ˆ็Žฐ๏ผŒๆ”ฏๆŒ็ปง็ปญๅฏน่ฏไผ˜ๅŒ–

็›ธๅ…ณๆŠ€่ƒฝ