From: sparhawk AT eunet DOT at (Gerhard Gruber) Newsgroups: comp.os.msdos.djgpp Subject: Re: "delete" and "delete []" operators Date: Fri, 10 Jul 1998 14:36:25 GMT Organization: Customer of EUnet Austria Lines: 52 Message-ID: <35a71a7e.2075466@news.Austria.EU.net> References: <199807092012 DOT QAA25515 AT delorie DOT com> NNTP-Posting-Host: e200.dynamic.vienna.at.eu.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Content-Transfer-Encoding: 8bit Precedence: bulk Destination: DJ Delorie From: Gruber Gerhard Group: comp.os.msdos.djgpp Date: Thu, 9 Jul 1998 16:12:12 -0400 (EDT): >You should use delete [] x, but for obscure reasons. Here's some >background. > >The difference between delete and delete [] can be best described by >this example: > > #include > > class Foo > { > public: > ~Foo() { printf("~Foo!\n"); }; > }; > > int main(void) > { > Foo *foo1 = new Foo[5]; > Foo *foo2 = new Foo[5]; > > printf("delete foo1\n"); > delete foo1; > printf("delete [] foo2\n"); > delete [] foo2; > > return 0; > } > >In both cases, an array of objects is created. However, only in the >delete [] case will all the destructors be called. > >The obscure part is that some compilers manage this by allocating an >additional bit of memory to keep track of the number of objects in the >array, and free this memory in delete []. If you call delete instead >of delete[], that memory is never freed and you have a memory leak. Does this mean that, in your above example, for each object in the array is the constructor called by new[] and the destructor by delete[]? -- Bye, Gerhard email: sparhawk AT eunet DOT at g DOT gruber AT sis DOT co DOT at Spelling corrections are appreciated.