home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!hexnut!jimad
- From: jimad@microsoft.com (Jim Adcock)
- Subject: Re: Should char arrays be deleted with delete[]?
- Message-ID: <1992Dec23.215018.25408@microsoft.com>
- Date: 23 Dec 92 21:50:18 GMT
- Organization: Microsoft Corporation
- References: <1992Dec18.161338.28124@promis.com>
- Keywords: C++
- Lines: 26
-
- 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 ?
-
- One rule applies to objects of any type:
-
- Foo* fooArray = new Foo[10];
- delete [] fooArray;
-
- Foo* pFoo = new Foo;
- delete pFoo;
-
- Base* pBase = new Foo; //assuming Base a public base class of Foo
- delete pBase; //AND that Base has a virtual destructor declared
-
- Base* baseArray = new Foo[10]; //WRONG!
- delete [] baseArray; //likewise wrong!
-
- While if you follow other rules your code might accidentally
- work sometimes on some implementations, then again, it might not.
-
-