home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18242 < prev    next >
Encoding:
Text File  |  1992-12-21  |  2.0 KB  |  50 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!brunix!brunix!sdm
  3. From: sdm@cs.brown.edu (Scott Meyers)
  4. Subject: Re: Should char arrays be deleted with delete[]?
  5. Message-ID: <1992Dec19.190916.29194@cs.brown.edu>
  6. Keywords: C++
  7. Sender: news@cs.brown.edu
  8. Organization: Brown University Department of Computer Science
  9. References: <1992Dec18.161338.28124@promis.com> <78593@hydra.gatech.EDU>
  10. Date: Sat, 19 Dec 1992 19:09:16 GMT
  11. Lines: 37
  12.  
  13. In article <78593@hydra.gatech.EDU> andy@federation.gatech.edu (Andy Register) writes:
  14. | In article <1992Dec18.161338.28124@promis.com> fogel@promis.com (Richard Fogel) writes:
  15. | >Should char strings allocated as follows:
  16. | >
  17. | >char *str = new char[10];
  18. | >
  19. | >later be deleted by:
  20. | >
  21. | >delete []str   OR    delete str  ?
  22. | To quote from Meyers, "In the case of an object type like char that has no
  23. | destructor, this issue isn't important, but in general this is something
  24. | you do not want to forget.  *Always* make sure that you use [] with 
  25. | delete if and only if you used [] with the corresponding call to new."
  26.  
  27. Unfortunately, Meyers is wrong in this case.  I know because I'm Meyers.
  28. Sigh.
  29.  
  30. The ARM is actually quite clear on this matter: regardless of the object
  31. types involved (including built-in types like int and char), you must use
  32. the [] form of delete when deleting arrays.  If you don't, the behavior of
  33. your program is undefined.  That being the case, the general rule is
  34. simple, has no exceptions, and (I am relieved to report) is correct in my
  35. book (p. 19):
  36.  
  37.   If you use [] when you call new, you must use [] when you call delete.
  38.   If you don't use [] when you call new, don't use [] when you call delete.
  39.  
  40. I've added the mis-statement above to the errata list for my book,
  41. "Effective C++."  You can get a copy of the errata list via anonymous ftp
  42. from wilma.cs.brown.edu.  The file is pub/Effective_C++_errata.txt.
  43.  
  44. Scott
  45.  
  46.  
  47. -------------------------------------------------------------------------------
  48. What do you say to a convicted felon in Providence?  "Hello, Mr. Mayor."
  49.