home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
- From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
- Subject: Re: Garbage Collection for C++
- Message-ID: <9223019.19999@mulga.cs.mu.OZ.AU>
- Sender: news@cs.mu.OZ.AU
- Organization: Computer Science, University of Melbourne, Australia
- References: <1992Aug13.153211.16572@ericsson.se> <1992Aug14.101226.6929@ericsson.se> <1992Aug15.005542.3104@news.mentorg.com> <1992Aug16.052233.23166@ucc.su.OZ.AU>
- Date: Mon, 17 Aug 1992 09:35:02 GMT
- Lines: 72
-
- maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
-
- >In article <1992Aug15.005542.3104@news.mentorg.com> bcannard@hppcb36.mentorg.com (Bob Cannard @ PCB x5565) writes:
- >>
- >>Reference counting suffers serious drawbacks:
- >>
- >>1. It can't handle cyclic data structures
- >>
- >>2. It is one of the most inefficient forms of garbage collection known to man.
- >>
- >>The main reason I'm pushing for true garbage collection is that I consider
- >>reference counting (which I already have) to be an unacceptably poor solution.
- >
- > Mm. Reference counting used to implement 'write on demand' in
- >copy contructors is supposedly inefficient. But this is not at all
- >the same as reference counting for garbage collection. In copy contructors,
- >you have to check the reference count before each write operation,
- >and if its not 1, clone off a copy of the object. Thats slow.
- >
- > But for GC you only have to increment the reference count
- >when you take the address of the object to form a GC pointer,
- >decrement it when the pointer is destroyed, and destroy the
- >object when the count goes to 0. That doesn't seem to be so slow
- >to me: please correct me if I'm wrong.
-
- OK, you're wrong, so I'll correct you :-)
-
- The problem is that a pointer may be destroyed everytime you assign
- a new value to a pointer variable.
-
- Consider the following example:
-
- ListPtr p;
- ...
- register int sum = 0;
- while(p) {
- sum += p->data;
- p = p->next; // assign new value to pointer
- }
-
- Now if p is simply a (ListNode *) then this will be very efficient.
- If p is a pointer to a reference-counted list node, then each assignment
-
- ListNode *p;
- ...
- p = p->next;
-
- becomes something like this:
-
- struct RefCountedListPtr {
- int refs;
- ListNode *value;
- } *p;
- ...
- if (p) {
- if (--p->refs == 0) {
- delete p->value;
- }
- }
- p = p->value->next;
- if (p) {
- p->refs++;
- }
-
- So (at the very least) every pointer assignment now requires two additional
- conditionals.
-
- --
- Fergus Henderson fjh@munta.cs.mu.OZ.AU
- This .signature VIRUS is a self-referential statement that is true - but
- you will only be able to consistently believe it if you copy it to your own
- .signature file!
-