How do we implement Garbage Collection In C++

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...
garbage collector?? as in.... what??
Make sure memory management is done by containers where applicable and smart pointers in all other cases.
Containers?? can u explain a bit more please?
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?
automatic memory management of a guest language (e.g. Java) implemented in the host language (e.g. C++)
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.
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.
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.
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
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.