How to implement a garbage collector in C ++

Possible duplicate:
How to implement garbage collection in C ++

I recently asked this question in an interview on how to implement a garbage collector in C ++.

My answer was to have a pre-allocated memory pool and build an object in this allocated space. Also for storing the size of memory allocated for an object in a byte preceding the memory location pointed to by the pointer.

The interviewer was not satisfied with the answer.

Later I realized that my solution was actually trying to avoid the main goal of the garbage collector by first allocating the memory pool and working with this memory.

But I think it would be difficult to implement a garbage collector in C ++ without the need to modify the compiler.

Any suggestions? Thanks in advance.

EDIT It seems that someone else also faced a similar problem, and many smart guys shed their views here

+5
source share
2 answers

You can read about shared_ptr structure.

It implements a simple reference-counting garbage collector.

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

Create a structure similar to shared_ptr, name it Object.

This will create a new object. Now with the overload of your operators, you can control the GC.

All you have to do now is simply implement one of the GC algorithms

+2

, , , . GC CS.

+2

All Articles