C Example: Leak Checker: live block and byte accounting
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.
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
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