C 例:文字列インターン:重複排除・参照カウント・ポインタ比較
最大16件のインターン表を維持します。同じ内容なら既存ポインタを再利用して refs を増やし、違うなら malloc したコピーを登録。hello・world・hello・C・world の5回後、ポインタ比較で同一文字列が同じアドレスを共有することを示し、最後に一括解放します。
🖥 オンライン実行GCC 10.2 · C11 標準 · ▶ を押して実行 · コードをコピー
🖨 参考出力標準 C11 環境でのオンライン実行結果と一致
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
🧩 学習を続ける