home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18375 < prev    next >
Encoding:
Text File  |  1992-12-23  |  1.0 KB  |  38 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Should char arrays be deleted with delete[]?
  5. Message-ID: <1992Dec23.215018.25408@microsoft.com>
  6. Date: 23 Dec 92 21:50:18 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec18.161338.28124@promis.com>
  9. Keywords: C++
  10. Lines: 26
  11.  
  12. In article <1992Dec18.161338.28124@promis.com> fogel@promis.com (Richard Fogel) writes:
  13. |Should char strings allocated as follows:
  14. |
  15. |char *str = new char[10];
  16. |
  17. |later be deleted by:
  18. |
  19. |delete []str   OR    delete str  ?
  20.  
  21. One rule applies to objects of any type:
  22.  
  23. Foo* fooArray = new Foo[10];
  24. delete [] fooArray;
  25.  
  26. Foo* pFoo = new Foo;
  27. delete pFoo;
  28.  
  29. Base* pBase = new Foo;     //assuming Base a public base class of Foo
  30. delete pBase;        //AND that Base has a virtual destructor declared
  31.  
  32. Base* baseArray = new Foo[10];    //WRONG!
  33. delete [] baseArray;            //likewise wrong!
  34.  
  35. While if you follow other rules your code might accidentally 
  36. work sometimes on some implementations, then again, it might not.
  37.  
  38.