C Example: Leak Checker: live block and byte accounting

🗓 Last updated: 2026-11-30👤 Written by: Ivy✓ Reviewed by: Alex🏷 Category: Memory & Pointers

Wraps malloc/free with a Header before each block recording its size, tracks live block and byte counts, and prints the running totals on every alloc/free. Allocates three blocks, frees two, intentionally leaks one, and reports final live blocks and bytes — a minimal leak detector.

⚡ Intermediate Reference entries: fn-mallocfn-freekw-sizeof
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
alloc  20 bytes -> live=1 bytes=20
alloc  32 bytes -> live=2 bytes=52
alloc  32 bytes -> live=3 bytes=84
a[4]=16 b="tracked buffer" c[3]=4.5
free   32 bytes -> live=2 bytes=52
free   20 bytes -> live=1 bytes=32
--- intentional leak of c ---
final live blocks=1 bytes=32
🧩 Keep Learning

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏