home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!taumet!steve
- From: steve@taumet.com (Steve Clamage)
- Subject: Re: Using delete with arrays - help please!
- Message-ID: <1992Nov16.175013.16714@taumet.com>
- Organization: TauMetric Corporation
- References: <1992Nov16.023852.24205@gopher.dosli.govt.nz>
- Date: Mon, 16 Nov 1992 17:50:13 GMT
- Lines: 35
-
- srlncnc@gopher.dosli.govt.nz (Chris Crook) writes:
-
- >My understanding is that I can allocate an array of class instances using
- >the syntax
- > MyClass *arrayOfInstances = new MyClass[arraySize];
- >and that when I want to delete it I use
- > delete [] arrayOfInstances
-
- Correct (I added the missing "*" to the original example).
-
- >What I am puzzled about is the wide use of arrays of characters in
- >the Borland code examples in which the arrays are allocated as
- > char *aBuffer = new char[bufferSize];
- > delete aBuffer;
-
- >Can you tell me why this works? Why doesn't it need the [] after the
- >delete operator?
-
- The effect of the second example is undefined. Portable programs
- must use the array form of "delete" when and only when the array form
- of "new" was used to allocate the array.
-
- In some implementations, the array form of "delete" does nothing
- special when the array base type has no destructor. In such
- implementations you would get the same effect with either of
- delete aBuffer;
- delete [] aBuffer;
- because type "char" has no destructor.
-
- You cannot rely on this behavior, however, and any sort of error might
- occur as the result of mixing the two styles of "new" and "delete".
- --
-
- Steve Clamage, TauMetric Corp, steve@taumet.com
- Vice Chair, ANSI C++ Committee, X3J16
-