home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13057 < prev    next >
Encoding:
Text File  |  1992-08-29  |  2.0 KB  |  62 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!uunet.ca!frumious!pat
  3. From: pat@frumious.uucp (Patrick Smith)
  4. Subject: Re: Garbage Collection for C++
  5. Message-ID: <1992Aug29.190009.411@frumious.uucp>
  6. Date: Sat, 29 Aug 1992 19:00:09 GMT
  7. Reply-To: uunet.ca!frumious!pat
  8. References: <DAVEG.92Aug27002517@synaptx.synaptics.com> <1992Aug29.054113.917@frumious.uucp> <TMB.92Aug29174821@arolla.idiap.ch>
  9. Organization: None
  10. Lines: 50
  11.  
  12. tmb@idiap.ch writes:
  13. |In article <1992Aug29.054113.917@frumious.uucp> pat@frumious.uucp (Patrick Smith) writes:
  14. |   Actually, I don't think adding GC to C++ would solve the problem
  15. |   of when to destroy temporaries.
  16. |
  17. |   GC could help with cases such as this:
  18. |
  19. |      String s, t;
  20. |      const char* p = (const char*) (s + t);
  21. |      // ... do something with p
  22. |
  23. |If GC were added to the C++ language, this would still not work.
  24. |You would need to write something like:
  25. |
  26. |    String s,t;
  27. |    const char *p = (const char *)new String(s+t);
  28.  
  29. What I had in mind was that one would write the String class so that
  30. the actual character array was stored in garbage collected memory.
  31. The String destructor would not release this memory; that would be
  32. left for the garbage collection mechanism to do.  In that case, I
  33. would expect the code I gave to work correctly.
  34.  
  35. Note that I did not say that GC would solve the problem itself; I just
  36. said that it would help with the solution.
  37.  
  38.  
  39. |If you wanted to do something like this frequently, you might be able
  40. |to define a class HeapString that eliminates the need for an explicit
  41. |call to "new".
  42.  
  43. By this do you mean something along the lines of my suggestion above?
  44.  
  45.  
  46. |   but I don't see how it would help with this:
  47. |
  48. |      ofstream("some_file") << "Hello, " << "world!\n";
  49. |
  50. |This code is wrong, and it would continue to be wrong in the presence
  51. |of GC.
  52.  
  53. It's wrong because of the current rules for destruction of temporaries.
  54. Changing those rules _might_ make this work as the programmer
  55. expected.  Just adding GC would not.  That was my point.
  56. I take it you agree.
  57.  
  58. -- 
  59. Patrick Smith
  60. uunet.ca!frumious!pat
  61. pat%frumious.uucp@uunet.ca
  62.