home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16370 < prev    next >
Encoding:
Text File  |  1992-11-16  |  1.6 KB  |  46 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Using delete with arrays - help please!
  5. Message-ID: <1992Nov16.175013.16714@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Nov16.023852.24205@gopher.dosli.govt.nz>
  8. Date: Mon, 16 Nov 1992 17:50:13 GMT
  9. Lines: 35
  10.  
  11. srlncnc@gopher.dosli.govt.nz (Chris Crook) writes:
  12.  
  13. >My understanding is that I can allocate an array of class instances using
  14. >the syntax
  15. >   MyClass *arrayOfInstances = new MyClass[arraySize];
  16. >and that when I want to delete it I use
  17. >   delete [] arrayOfInstances
  18.  
  19. Correct (I added the missing "*" to the original example).
  20.  
  21. >What I am puzzled about is the wide use of arrays of characters in
  22. >the Borland code examples in which the arrays are allocated as
  23. >   char *aBuffer = new char[bufferSize];
  24. >   delete aBuffer;
  25.  
  26. >Can you tell me why this works? Why doesn't it need the [] after the
  27. >delete operator?
  28.  
  29. The effect of the second example is undefined.  Portable programs
  30. must use the array form of "delete" when and only when the array form
  31. of "new" was used to allocate the array.
  32.  
  33. In some implementations, the array form of "delete" does nothing
  34. special when the array base type has no destructor.  In such
  35. implementations you would get the same effect with either of
  36.     delete aBuffer;
  37.     delete [] aBuffer;
  38. because type "char" has no destructor.
  39.  
  40. You cannot rely on this behavior, however, and any sort of error might
  41. occur as the result of mixing the two styles of "new" and "delete".
  42. -- 
  43.  
  44. Steve Clamage, TauMetric Corp, steve@taumet.com
  45. Vice Chair, ANSI C++ Committee, X3J16
  46.