rpj@redcloud.cad.mcc.com (Rich Johns) (06/16/89)
Assume:
class Foo {
Foo() { /*stuff*/}
~Foo() { /* stuff*/}
//stuff
};
main() {
Foo** fooArray = nil;
fooArray = new Foo*[100];
// do something
}
My question has to do with deallocating fooArray. If I am not concerned with
deleting the contents of fooArray (the 100 Foos), it seems like:
delete fooArray;
is correct. If I did want to delete the 100 Foos, would this be correct:
delete [100]fooArray;
or would I have to:
for (int i = 0; i < 100; i++) {
delete fooArray[i];
}
delete fooArray;
Would the answer be different if Foo does not have a
constructor and destructor?
thanks.
Rich Johns, MCC CAD Program | 3500 W. Balcones Center Dr., Austin, TX 78759
ARPA: johns@mcc.com | Phone: [512] 338-3714
UUCP: {uunet,harvard,gatech,pyramid}!cs.utexas.edu!milano!cadillac!johnsshopiro@alice.UUCP (Jonathan Shopiro) (06/17/89)
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
- deleting the contents of fooArray (the 100 Foos), it seems like:
Your problem is fooArray does not contain 100 Foos, it contains 100 pointers to
Foo.
-
- delete fooArray;
This works if the delete mechanism doesn't need to know how the size of
the object being deleted. (Universally true, I believe).
-
- is correct. If I did want to delete the 100 Foos, would this be correct:
-
- 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)
- delete fooArray;
This deletes the array of pointers. Use the more explicit
``delete [100] fooArray'' as a good habit.
-
- Would the answer be different if Foo does not have a
- constructor and destructor?
no.
--
Jonathan Shopiro
AT&T Bell Laboratories, Warren, NJ 07060-0908
research!shopiro (201) 580-4229dmg@ssc-vax.UUCP (David Geary) (06/20/89)
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" ~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
shopiro@alice.UUCP (Jonathan Shopiro) (06/21/89)
In article <2735@ssc-vax.UUCP>, dmg@ssc-vax.UUCP (David Geary) writes: > > In article <9500@alice.UUCP>, Jonathan Shopiro writes: > + In article <1245@cadillac.CAD.MCC.COM>, rpj@redcloud.cad.mcc.com (Rich Johns) writes: > ... > + - 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. Quite so. In the original example, they weren't null, they were uninitialized. I think it is unfortunate that the language guarantees that delete can be applied to null harmlessly (the compiler has to generate code to check for null every time) and I think it is bad form to apply delete to null pointers. b o r i n g b o r i n g -- Jonathan Shopiro AT&T Bell Laboratories, Warren, NJ 07060-0908 research!shopiro (201) 580-4229