Hildo Biersma wrote:

> Fisher Mark wrote:
> >
> > > One C++ problem I just found out is memory management.  It seems
> > > that it's impossible to 'new' an object from an specified memory block.
> > > So it's impossible to put free'd objects in memory pool and re-allocate
> > > them next time.
> >
> > It can't be done by the default new operator, but you can do it with an
> > overloaded new operator.  An overloaded operator new can do whatever you
> > need it to do -- I've got 9-year old code that does arena allocation and
> > reference counting by overloading new.
>
> It can be done using 'placement new', for which you do not have to
> override any operator.  See Stroustrup 3rd edition, 10.4.11, 'Placement
> of objects".

I finally read some books and understand how to do that in C++! Thanks you
guys very much!

Also, I found a GREAT garbage collector for C and C++, which is written by
Hans Boehm,
and would like introduce to you guys.

It's using mark-sweep algorithm to maintan memory, although Perl's gc is
count-reference.
But I think it's still very helpful for memory debugging, or other projects.

It's so smart that it can maintan active memory automatically by scanning
process's heap
and static memory, instead of asking you pass your global varible pointers.

Here is an example, it only uses fixed allocated memory block instead of using
up all your memory!
[yw@yw gc]$ more mytest.c
#include "gc.h"
main() {
 int i=0;
 while(++i) {
   void *p = GC_malloc(1000);
   if(i%15000==0) printf("%x\n", p);
 }
}
[yw@yw gc]$


Here is its URLs:
http://www.hpl.hp.com/personal/Hans_Boehm/gc/
http://www.hpl.hp.com/personal/Hans_Boehm/gc/gcdescr.html

>
>
> Cheers,
>   Hildo

--

Sincerely,

Ye, Wei



Reply via email to