C Example: Reference Counting: shared buffer with retain/release
Implements a ref-counted shared buffer: a struct header stores refs, a flexible array holds the text; retain increments and release decrements, freeing at zero. Demonstrates the full lifecycle of create, retain, two releases, and confirms both pointers address the same block.
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
create -> refs=1 data="shared payload" retain -> refs=2 alias points to same block: yes release -> refs=1 release -> refs=0 (freed) done, no buffer left
🧩 Keep Learning