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