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

C

Avoid common C mistakes โ€” memory leaks, buffer overflows, undefined behavior, and pointer traps.

ไธ‹่ฝฝ1.6k
ๆ˜Ÿๆ ‡2
็‰ˆๆœฌ1.0.1
AI ๆ™บ่ƒฝไฝ“
ๅฎ‰ๅ…จ้€š่ฟ‡

ๆŠ€่ƒฝ่ฏดๆ˜Ž


name: C slug: c version: 1.0.1 description: Write safe C avoiding memory corruption, buffer overflows, and undefined behavior traps. metadata: {"clawdbot":{"emoji":"โš™๏ธ","requires":{"bins":["gcc","clang"]},"os":["linux","darwin","win32"]}}

Quick Reference

TopicFile
malloc/free, leaks, double freememory.md
Null, dangling, pointer arithmeticpointers.md
Null terminator, buffer overflowstrings.md
Integer overflow, signed/unsignedtypes.md
Macro traps, include guardspreprocessor.md
Common undefined behaviorundefined.md

Critical Rules

  • malloc returns void* โ€” cast required in C++, optional in C but check for NULL
  • free(ptr); ptr = NULL; โ€” always null after free to prevent double-free
  • sizeof(array) in function gives pointer size, not array size โ€” pass length separately
  • char str[5] = "hello"; โ€” no room for null terminator, UB when used as string
  • strcpy doesn't check bounds โ€” use strncpy and manually null-terminate
  • Signed overflow is UB โ€” compiler can optimize assuming it never happens
  • i++ + i++ is UB โ€” no sequence point between modifications
  • Returning pointer to local variable โ€” dangling pointer, UB on use
  • #define SQUARE(x) x*x โ€” SQUARE(1+2) = 1+2*1+2 = 5, not 9
  • memcpy with overlapping regions โ€” use memmove instead
  • Uninitialized variables โ€” contain garbage, UB if used
  • Array out of bounds โ€” no runtime check, silent corruption or crash

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

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

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