home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: (none)
- Message-ID: <1992Sep4.161836.17100@taumet.com>
- Organization: TauMetric Corporation
- References: <Bu16wq.L6E@knot.ccs.queensu.ca>
- Date: Fri, 4 Sep 1992 16:18:36 GMT
- Lines: 45
-
- rollins@bart.ee.queensu.ca (Mark Rollins) writes:
-
-
- >Using the syntax of Stroustrup's 1st edition, pg. 162
- >and g++ version 2.2.2, libg++ version 2.1, and SunOS 4.2.2 I get
- >correct results but a compiler warning:
-
- > g++ -o bin/junk badmem.cc -lg++ -lm
- > badmem.cc: In method `Vector::~Vector ()':
- > badmem.cc:9: warning: use of array size with vector delete is anachronistic
-
- >If I omit the array size, the Vector does not get de-allocated
- >properly using the destructor, and all virtual memory gets eaten up
- >rather quickly. (It's probably just deleting the 1st one in the array).
-
- >What is the proper syntax for deleting a vector of class objects ?
-
- Don't use Stroustrup 1st edition as a language reference. It is of
- only historical interest now, as there have been many important
- language changes since 1985.
-
- Handling of arrays of classes is one area where the language has
- changed, and it depends on the particular release of the C++
- compiler you use.
-
- With all modern compilers, an array of objects allocated as
- p = new T [count]; // T is some type
- is deleted like this:
- delete [] p; // empty brackets
-
- If a single object was allocated, not an array, you use
- delete p; // no brackets
-
- If you use "delete p" for an array, or "delete [] p" for a single
- object, the results are undefined. The program may appear to work,
- or you might get a runtime error message of some kind or a system crash.
-
- Some compilers accept an expression in the delete brackets
- delete [count] p; // obsolete
- but ignore or only warn about the expression. Compilers which
- require the count are obsolete and should be replaced.
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-