How to implement garbage collection in C++

前端 未结 6 1355
攒了一身酷
攒了一身酷 2020-12-12 10:15

I saw some post about implement GC in C and some people said it\'s impossible to do it because C is weakly typed. I want to know how to implement GC in C++.

I want s

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-12 11:07

    You can read about the shared_ptr struct.

    It implements a simple reference-counting garbage collector.

    If you want a real garbage collector, you can overload the new operator.

    Create a struct similar to shared_ptr, call it Object.

    This will wrap the new object created. Now with overloading its operators, you can control the GC.

    All you need to do now, is just implement one of the many GC algorithms

提交回复
热议问题