home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12413 < prev    next >
Encoding:
Text File  |  1992-08-16  |  2.6 KB  |  84 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!cs.mu.OZ.AU!munta.cs.mu.OZ.AU!fjh
  3. From: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON)
  4. Subject: Re: Garbage Collection for C++
  5. Message-ID: <9223019.19999@mulga.cs.mu.OZ.AU>
  6. Sender: news@cs.mu.OZ.AU
  7. Organization: Computer Science, University of Melbourne, Australia
  8. References: <1992Aug13.153211.16572@ericsson.se> <1992Aug14.101226.6929@ericsson.se> <1992Aug15.005542.3104@news.mentorg.com> <1992Aug16.052233.23166@ucc.su.OZ.AU>
  9. Date: Mon, 17 Aug 1992 09:35:02 GMT
  10. Lines: 72
  11.  
  12. maxtal@extro.ucc.su.OZ.AU (John MAX Skaller) writes:
  13.  
  14. >In article <1992Aug15.005542.3104@news.mentorg.com> bcannard@hppcb36.mentorg.com (Bob Cannard @ PCB x5565) writes:
  15. >>
  16. >>Reference counting suffers serious drawbacks:
  17. >>
  18. >>1. It can't handle cyclic data structures
  19. >>
  20. >>2. It is one of the most inefficient forms of garbage collection known to man.
  21. >>
  22. >>The main reason I'm pushing for true garbage collection is that I consider
  23. >>reference counting (which I already have) to be an unacceptably poor solution.
  24. >
  25. >    Mm. Reference counting used to implement 'write on demand' in
  26. >copy contructors is supposedly inefficient. But this is not at all
  27. >the same as reference counting for garbage collection. In copy contructors,
  28. >you have to check the reference count before each write operation,
  29. >and if its not 1, clone off a copy of the object. Thats slow.
  30. >
  31. >    But for GC you only have to increment the reference count
  32. >when you take the address of the object to form a GC pointer,
  33. >decrement it when the pointer is destroyed, and destroy the 
  34. >object when the count goes to 0. That doesn't seem to be so slow
  35. >to me: please correct me if I'm wrong.
  36.  
  37. OK, you're wrong, so I'll correct you :-)
  38.  
  39. The problem is that a pointer may be destroyed everytime you assign
  40. a new value to a pointer variable.
  41.  
  42. Consider the following example:
  43.  
  44.     ListPtr p;
  45.     ...
  46.     register int sum = 0;
  47.     while(p) {
  48.         sum += p->data;
  49.         p = p->next;        // assign new value to pointer
  50.     }
  51.  
  52. Now if p is simply a (ListNode *) then this will be very efficient.
  53. If p is a pointer to a reference-counted list node, then each assignment
  54.  
  55.         ListNode *p;
  56.         ...
  57.         p = p->next;
  58.  
  59. becomes something like this:
  60.  
  61.         struct RefCountedListPtr {
  62.             int refs;
  63.             ListNode *value;
  64.         } *p;
  65.         ...
  66.         if (p) {
  67.             if (--p->refs == 0) {
  68.                 delete p->value;
  69.             }
  70.         }
  71.         p = p->value->next;
  72.         if (p) {
  73.             p->refs++;
  74.         }
  75.  
  76. So (at the very least) every pointer assignment now requires two additional
  77. conditionals.
  78.  
  79. -- 
  80. Fergus Henderson             fjh@munta.cs.mu.OZ.AU      
  81. This .signature VIRUS is a self-referential statement that is true - but 
  82. you will only be able to consistently believe it if you copy it to your own
  83. .signature file!
  84.