views:

188

answers:

4

hello, i've been reading about memory allocation and c++ programming in general.. my boss thinks it is a good idea to try and design a simple garbage collector to enhance my learning ( i always believed.. it is better to code a project, than to do a generalized written code, to test functions, because it helps understand the exact usage of functions)..

so anyway.. i read about garbage collectors and i understand "generally" what they do..and why we need them.. what i need is for some1 to point out to me..where to start programming..:S or maybe a book/tutorial.. that would help me start the work..

Edit: i know it is not the best place to start learning c++ .. but i have the basic knowledge covered (university courses) , and been playing alittle with winapis.. i am sorry for the misunderstanding on the "and c++ programming in general".. what i am trying to do is enhance my coding skills and professinality ..

+4  A: 

Richar Jones's Garbage Collection Page references lot of ressource, included a book.

Note that in practice, writing a garbage collector means entering deep in the implementation dependent realm. I'm not sure it is the best way of learning to program in C++ in general.

AProgrammer
+1 For the note on, not the bets place to start learning C++. Although this type of project is certainly interesting, it really isn't a good place to learn the general concepts of C++.
DeusAduro
@Aprogrammer: i am sorry for the misunderstanding.. i am beyond the c++ basics.. i am trying to enhance my coding skills, professinality and design..
Madi D.
Doing a simple GC will not bring you very much. And it would be unusable in practice. Doing an usuable one is starting a journey in very low level stuff, depending as well on the compiler, the OS and the HW architecture. Interesting, but not the first thing I'd do.
AProgrammer
hmm.. could u suggest an alternative :)?
Madi D.
What are your goals? What are your interests? What are the itches you have at work with no official time to handle them? (This is becoming one of the "what project to learn programming/C++/design ?" questions who always leave me wondering... I've always more ideas than time to explore them.)
AProgrammer
A: 

You could take a look at the shared_ptr in boost. It is considered as an explicit garbage collector.

AraK
thx, already checking it along with Hans_Boehm GChttp://www.hpl.hp.com/personal/Hans_Boehm/gc/
Madi D.
+2  A: 

I'm afraid that your boss is wrong. This is a misguided project for at least two reasons:

  • Firstly, there really is no such thing as simple garbage collector, particularly when you need to retrofit it to an existing language implementation.

  • Secondly, garbage collection does not play well with C++ because it cannot normally call destructors to free non-memory resources. There have been experiments in this area but none has been notably succesful.

If I were you, I would spend the time you would have spent on this project familiarising yourself with the concept of RAII, smart pointers and possibly memory pools, which fit better into the C++ way of doing things than GC ever can.

anon
For the second issue, Hans Boehm paper http://www.hpl.hp.com/techreports/2002/HPL-2002-335.ps is a must read.
AProgrammer
And for those of us without a PostScript device? Why to people do this, when PDF is an industry standard?
anon
And I forgot the obvious, there are design papers for introducing GC in C++ on the committee site (they aren't implementation related, just about what interface to use and how this interact with various C++ features).
AProgrammer
I agree. I think writing your own custom memory allocator is much more relevant and applicable for C++
zebrabox
AProgrammer
A: 

I think you first have to define at which level you want to set your "exercise". Writting a true garbage collector might be a tremendeous task. Real ones are written for new languages...

Why ? because to do garbage collection, you have to control the memory management of what you want to garbage (memory allocation/deallocation). When you write a new language, you define memory management.

If the goal is not to write a new language but to practise c++ you first have to decide at which level of memory management you want to work.

You can design a simple memory allocation procedure with reference counting. You can then use a simple garbage collector algorithm that automatically delete dereferenced objects. Writting such a system is quite simple but a real good exercise. You will find that in real world using a smart shared pointer (see boost) is the same thing in principle, but a best one.

Going further is not simple. As I have said you have to deal with memory management. There is no simple/portable/C++ way to do that.

Then my advice is : write a simple object allocation and a simple garbage collector algorithm (reference counting). Then look at boost source code to learn a state of the art shared pointer implementation. And if in the future your need is not to do some c++ practise but you need some C++ automatic memory management, use smart pointers ... unless you want to write a new language !

my 2 cents.

neuro
thx neuro :) ur advice is much appreciated
Madi D.
No problem. Do not hesitate to click on the "This answer is helpful" button :)
neuro
actually i am choosing ur answer :)..already started to code it..and it is as every1 said..tough!!!!!!!!!!!!still a good challange :)
Madi D.