Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!lll-winken!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!casbah.acns.nwu.edu!nucsrl!tellab5!balr!clrcom!rmartin From: rmartin@clear.com (Bob Martin) Newsgroups: comp.lang.c++ Subject: Re: new/dispose behavior Summary: Clarification of delete operator for arrays. Message-ID: <1991May1.124939.3963@clear.com> Date: 1 May 91 12:49:39 GMT References: <1619@msa3b.UUCP> Organization: Clear Communications, Inc. Lines: 41 In article <1619@msa3b.UUCP> kevin@msa3b.UUCP (Kevin P. Kleinfelter) writes: >I'm reading along in a book about C++, and I see that one can do: > > char *s; > s = new char [40]; > ... > delete s; > >The book then goes on to say that one should: > > int *p; > p = new int [40]; > ... > delete 40 p; > >True or False: If one wants to delete all of the 'new-ed' storage one need > not specify the quantity to delete, just the pointer to the > storage. Kevin: In the ARM section 5.3.4 there is a discussion about delete and arrays. You must use the form delete [] otherwise the operation is "undefined" and you will probably encounter odd execution errors. char *s; s = new char [40]; ... delete [] s; And the same goes for ints, longs, doubles, classes, etc. So the answer to your query is "True" but with the added qualifier that when deleting an array one must use the delete [] form of the operator. -- +-Robert C. Martin-----+:RRR:::CCC:M:::::M:| Nobody is responsible for | | rmartin@clear.com |:R::R:C::::M:M:M:M:| my words but me. I want | | uunet!clrcom!rmartin |:RRR::C::::M::M::M:| all the credit, and all | +----------------------+:R::R::CCC:M:::::M:| the blame. So there. |