C Example: String Interning: de-duplication, refcounts and pointer equality
Maintains an intern table of up to 16 entries: identical content reuses the existing pointer and increments refs, otherwise a malloc'd copy is registered. After interning hello, world, hello, C and world, pointer comparison proves identical strings share one address; all entries are released at the end.
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
malloc "hello" (refs=1, slot=0) malloc "world" (refs=1, slot=1) reuse "hello" (refs=2, slot=0) malloc "C" (refs=1, slot=2) reuse "world" (refs=2, slot=1) a==c: same pointer b==e: same pointer a==b: different pointers d starts with C: yes interned strings: 3
🧩 Keep Learning