home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13048 < prev    next >
Encoding:
Internet Message Format  |  1992-08-29  |  3.6 KB

  1. Path: sparky!uunet!sun-barr!olivea!mintaka.lcs.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Garbage Collection for C++
  5. Message-ID: <TMB.92Aug29174821@arolla.idiap.ch>
  6. Date: 29 Aug 92 21:48:21 GMT
  7. References: <DAVEG.92Aug20022559@synaptx.synaptics.com>
  8.     <1992Aug25.183619.9541@microsoft.com>
  9.     <DAVEG.92Aug27002517@synaptx.synaptics.com>
  10.     <1992Aug29.054113.917@frumious.uucp>
  11. Sender: news@ai.mit.edu
  12. Reply-To: tmb@idiap.ch
  13. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  14.     Perceptive)
  15. Lines: 84
  16. In-reply-to: pat@frumious.uucp's message of 29 Aug 92 05:41:13 GMT
  17.  
  18. In article <1992Aug29.054113.917@frumious.uucp> pat@frumious.uucp (Patrick Smith) writes:
  19.  
  20.    daveg@synaptics.com (Dave Gillespie) writes:
  21.    |Every third posting in the destruction-of-temporaries thread
  22.    |in this group ends with "if only we had GC so we could solve this
  23.    |problem once and for all..."
  24.  
  25.    Actually, I don't think adding GC to C++ would solve the problem
  26.    of when to destroy temporaries.
  27.  
  28. It would help. It wouldn't solve all the problems, and it wouldn't
  29. solve them automatically.
  30.  
  31.    GC could help with cases such as this:
  32.  
  33.       String s, t;
  34.       const char* p = (const char*) (s + t);
  35.       // ... do something with p
  36.  
  37. If GC were added to the C++ language, this would still not work.
  38. You would need to write something like:
  39.  
  40.     String s,t;
  41.     const char *p = (const char *)new String(s+t);
  42.  
  43. If you wanted to do something like this frequently, you might be able
  44. to define a class HeapString that eliminates the need for an explicit
  45. call to "new".
  46.  
  47.    but I don't see how it would help with this:
  48.  
  49.       ofstream("some_file") << "Hello, " << "world!\n";
  50.  
  51. This code is wrong, and it would continue to be wrong in the presence
  52. of GC.
  53.  
  54. As an aside, even in CommonLisp implementations, it is usually bad to
  55. write something like:
  56.  
  57.     (let ((stream (open ...)))
  58.        (princ "Hello, world\n" stream))
  59.        
  60. Instead, what you write is:
  61.  
  62.     (with-open-stream (stream (open ...))
  63.            (princ "Hello, world\n"))
  64.  
  65. The macro "with-open-stream" makes sure that the stream is closed
  66. immediately when the block is exited.
  67.  
  68. In fact, the CommonLisp language (last I checked) did not even
  69. guarantee that left-open file descriptors were closed by the GC.
  70.  
  71.    Well, there is one way.  One could require that all temporaries be
  72.    allocated and destroyed by the garbage collection mechanism.
  73.  
  74.    There are at least two problems with this:
  75.  
  76.    1) The destructors for the temporaries are run arbitrarily late,
  77.       if indeed they are ever run (this depends on the rules GC is
  78.       subject to).  In the above example, the programmer probably
  79.       wanted the ofstream destructor to be run fairly soon.
  80.  
  81. As I have argued before, the GC should not call destructors.  The
  82. primary purpose of GC is to reclaim memory that is not accessible
  83. anymore. All that the GC does, therefore, is give you the illusion
  84. that you have infinite amounts of memory, without any user-visible
  85. effects.
  86.  
  87. It is usually not the right thing to use the GC to reclaim external
  88. resources that are not accessible anymore, since such resources tend
  89. to be scarce, and (as you also observed) since destruction of such
  90. resources usually does have user-visible effects.
  91.  
  92.    2) Some (most? almost all?) applications would run more slowly if
  93.       temporaries were allocated from a garbage-collected heap instead
  94.       of from the stack.
  95.  
  96. Within the constraints of the C++ programming langauge, this is likely
  97. to be true, although there are efficient implementations of
  98. programming languages for which heap allocation is no more costly than
  99. stack allocation.
  100.  
  101.                     Thomas.
  102.