5

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

Community
  • 1
  • 1
Amm Sokun
  • 1,298
  • 4
  • 20
  • 35
  • 2
    There is an exact match for your query here: http://stackoverflow.com/questions/5009869/how-to-implement-garbage-collection-in-c Hope that helps – Dennis Jun 17 '11 at 09:09
  • 1
    Implement a garbage collector *in* C++, or implement a garbage collector *for* C++? If you've written your JVM, and the language you happen to have used to write it is C++, then you implement your Java garbage collector in C++ more or less the same way you'd implement a Java garbage collector in any language ;-) – Steve Jessop Jun 17 '11 at 09:16

2 Answers2

2

I think the interviewer was looking for smart pointers as that's the best the language can do and even then it requires a certain amount of attention from the programmer. This isn't proper GC in the CS sense.

Skizz
  • 69,698
  • 10
  • 71
  • 108
  • yep, to implement a GC for C++ on lines of JAVA GC would require compiler modification. So smart pointers is definitely one way. But isnt is correct to say that smart pointers does nothing more that memory pools using placement new, infact both are actually the same??? – Amm Sokun Jun 17 '11 at 09:19
  • @Amm: no, because smart pointers do more than that. In particular, what makes them interesting is that they automatically *free* the memory managed by them – jalf Jun 17 '11 at 09:45
2

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

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
  • And make sure the user doesn't allow a raw pointer to "escape" code that holds an Object, same as users of `shared_ptr` have to. Note that the Boehm collector is cleverer than this, it uses implementation-specific stack and object analysis to mark/sweep raw pointers rather than just special Objects. – Steve Jessop Jun 17 '11 at 09:20
  • overloading the new operator is what the memory pools in C++ do. Also i wonder to achieve reference counting in C++? – Amm Sokun Jun 17 '11 at 09:24