Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!ucbvax!decwrl!hplabs!hp-pcd!hplsla!jima From: jima@hplsla.HP.COM (Jim Adcock) Newsgroups: comp.lang.c++ Subject: Re: GC (was Re: Eiffel vs. C++ (vs Smalltalk)) Message-ID: <6590170@hplsla.HP.COM> Date: 24 Jun 89 00:27:53 GMT References: <14739@duke.cs.duke.edu> Organization: HP Lake Stevens, WA Lines: 77 The following simple program makes random length trees -- and fools Boehm's GC. I seem to remember something about this in his README file. Maybe one had better bone up on his algorithm before getting too deep in this. (change either 2 to a 3 (25% chance verses 50% chance) in randomlinks and the GC works fine. Program is not "publication quality.") ----------- #include #include "gc++.h" extern "C" { int rand(); }; void* ::operator new(long sz) { return gc_malloc((sz < 8) ? 8 : sz); } void ::operator delete(void *p) { if (p) gc_free(p); } class link { public: link* head; link* tail; link() {head = 0; tail = 0;} void randomlinks(); }; long sum; void link::randomlinks() { if (!(rand() & 2)) { head = new link; head->randomlinks(); sum += sizeof(link); } if (!(rand() & 2)) { tail = new link; tail->randomlinks(); sum += sizeof(link); } } main() { link * p[4096]; initialize_allocator(); long cntinc = 4096; int n, q; while (1) { q = 0x0ff & rand(); p[q] = new link; sum += sizeof(link); (p[q])->randomlinks(); if (sum > cntinc) { cntinc <<= 1; printf("\n *** total 'lost' so far: %d ***\n\n",sum); if (sum > 4000000) exit(0); } } }