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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: (none)
  5. Message-ID: <1992Sep4.161836.17100@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <Bu16wq.L6E@knot.ccs.queensu.ca>
  8. Date: Fri, 4 Sep 1992 16:18:36 GMT
  9. Lines: 45
  10.  
  11. rollins@bart.ee.queensu.ca (Mark Rollins) writes:
  12.  
  13.  
  14. >Using the syntax of Stroustrup's 1st edition, pg. 162
  15. >and g++ version 2.2.2, libg++ version 2.1, and SunOS 4.2.2 I get
  16. >correct results but a compiler warning:
  17.  
  18. >   g++ -o bin/junk badmem.cc -lg++ -lm
  19. >   badmem.cc: In method `Vector::~Vector ()':
  20. >   badmem.cc:9: warning: use of array size with vector delete is anachronistic
  21.  
  22. >If I omit the array size, the Vector does not get de-allocated
  23. >properly using the destructor, and all virtual memory gets eaten up
  24. >rather quickly. (It's probably just deleting the 1st one in the array).
  25.  
  26. >What is the proper syntax for deleting a vector of class objects ?
  27.  
  28. Don't use Stroustrup 1st edition as a language reference.  It is of
  29. only historical interest now, as there have been many important
  30. language changes since 1985.
  31.  
  32. Handling of arrays of classes is one area where the language has
  33. changed, and it depends on the particular release of the C++
  34. compiler you use.
  35.  
  36. With all modern compilers, an array of objects allocated as
  37.     p = new T [count]; // T is some type
  38. is deleted like this:
  39.     delete [] p; // empty brackets
  40.  
  41. If a single object was allocated, not an array, you use
  42.     delete p; // no brackets
  43.  
  44. If you use "delete p" for an array, or "delete [] p" for a single
  45. object, the results are undefined.  The program may appear to work,
  46. or you might get a runtime error message of some kind or a system crash.
  47.  
  48. Some compilers accept an expression in the delete brackets
  49.     delete [count] p; // obsolete
  50. but ignore or only warn about the expression.  Compilers which
  51. require the count are obsolete and should be replaced.
  52. -- 
  53.  
  54. Steve Clamage, TauMetric Corp, steve@taumet.com
  55. Vice Chair, ANSI C++ Committee, X3J16
  56.