How do we implement Garbage Collection In C++

Jul 2, 2010 at 3:13am
Hi Guys,
I have been off this forum for a couple of days. But I am back with a new question

How do we implement garbage collector in C++??

My initial thought is to use a smart pointer.. But was just wondering is there a better and efficient way of doing it!

Post in your thoughts here...
Jul 2, 2010 at 3:14am
garbage collector?? as in.... what??
Jul 2, 2010 at 3:15am
Make sure memory management is done by containers where applicable and smart pointers in all other cases.
Jul 2, 2010 at 4:21am
Containers?? can u explain a bit more please?
Jul 2, 2010 at 5:22am
What do you mean "garbage collection"? Automatic memory management of a program's own memory, automatic memory management of a guest language (e.g. Java) implemented in the host language (e.g. C++), or something else entirely?
Jul 2, 2010 at 5:55am
automatic memory management of a guest language (e.g. Java) implemented in the host language (e.g. C++)
Jul 2, 2010 at 8:30am
You'd need some kind of smart pointer that you must always pass by value (so the reference counts can be maintained).

If you think about how a Java object pointer is used, you'll have a clearer idea on what smarts you need to implement.
Jul 2, 2010 at 5:31pm
The correct answer is: don't. Garbage collection does not work well with C++.

Better to use RAII (which you can Google). Simply put, make sure that you have destructors to properly clean up all the resources you aquire.
Jul 2, 2010 at 5:32pm
You'd have to hire off some low-educated people and get a truck with those nifty steps on the back, just go for a ride and you've got your garbage collection in no time.
Jul 4, 2010 at 7:44am
Edit: Sorry, didn't read the thread.
If you're wondering how you actually make a garbage collection system, you may want to look at Lua.

The source code is available, and it has full garbage collection, not just reference counting. It's a real-world example but at the same time small enough that you can probably digest the gist of what it's doing with a couple days of tearing it apart.
Last edited on Jul 4, 2010 at 8:02pm
Jul 4, 2010 at 1:54pm
People, read the thread before posting. He doesn't want garbage collection C++, he wants garbage collection in a language that will be implemented in C++.
Topic archived. No new replies allowed.