Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!cornell!uw-beaver!fluke!ssc-vax!dmg From: dmg@ssc-vax.UUCP (David Geary) Newsgroups: comp.lang.c++ Subject: Re: deallocating an array of objects Message-ID: <2735@ssc-vax.UUCP> Date: 19 Jun 89 17:40:27 GMT Organization: Boeing Aerospace Corp., Seattle WA Lines: 45 In article <9500@alice.UUCP>, Jonathan Shopiro writes: + In article <1245@cadillac.CAD.MCC.COM>, rpj@redcloud.cad.mcc.com (Rich Johns) writes: + - + - Assume: + - + - class Foo { + - Foo() { /*stuff*/} + - ~Foo() { /* stuff*/} + - }; + - + - main() { + - Foo** fooArray = 0; + - fooArray = new Foo*[100]; // fooArray points to 100 uninitialized pointers + - + - // do something + - + - } + - + - My question has to do with deallocating fooArray. If I am not concerned with... + - delete [100]fooArray; + This works too, but it doesn't delete the pointed-to Foos. + - + - or would I have to: + - + - for (int i = 0; i < 100; i++) { + - delete fooArray[i]; + - } + This deletes the pointed-to Foos (and they better be there to delete) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ If the pointed-to Foos are not there, and each element of FooArray is NULL, will delete fooArray[i]; do any harm? I was under the impression that delete, when applied to NULL does nothing. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~ David Geary, Boeing Aerospace, Seattle ~ ~ "I wish I lived where it *only* rains 364 days a year" ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~