Possible Duplicate:
How to implement garbage collection in C++
I was recently asked this question in an interview about how to implement a garbage collector in c++.
My answer was to have a pre allocated memory pool and construct object in that allocated space. Also to store the size of the memory allocated to an object in the byte preceding the memory location at which the pointer is pointing to.
The interviewer wasnt satisfied by the answer.
I later realized that my solution was actually trying to avoid the main goal of garbage collector by preallocating a memory pool and working with that memory.
But i think it would be difficult to implement a garbage collector in C++ without having to modify the compiler.
Any suggestions? Thanks in advance!!!
EDIT It seems that someone else too came faced the similar problem and loads of brainy guys have shed their views here
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With