Article View: comp.std.c++
Article #3638Re: delete or delete[] ???
From: Claus A. Faerber
Date: Mon, 11 Mar 1996 00:42
Date: Mon, 11 Mar 1996 00:42
50 lines
2319 bytes
2319 bytes
David Sachs <b91926@fsgi01.fnal.gov> (07 Mar 96):
> Should delete or delete[] be used for a pointer to an array type?
>
> // It is assumed that some_type is an existing type
> typedef some_type array_type[10];
>
> array_type* a1 = new array_type[5];
> array_type* b1 = new some_type[5][10];
In both cases, you're creating a some_type[5][10], which is
an array_type[5] by typedef.
It is an array of array_types, sou you have to use delete[].
> array_type* a2 = new array_type;
> array_type* b2 = new some_type[10];
Here, you are creating a some_type[10] in both cases, which
is array_type.
It is one single array_type, not an array of array_types, so
you have to use delete without [] in both cases.
> ...
> delete[] a1;
> delete[] b1;
Ok, both point to arrays of the pointers' types.
> delete a2; // or should it be delete[] a2;
Ok, points to a single array_type.
> delete[] b2; // or should it be delete b2;
*No*, points also to a single array_type, so you actually
have to use delete b2.
Claus
------------------------------------------------------------------------
Claus Andre Faerber - claus@faerber.muc.de - http://www.muc.de/~cfaerber
------------------------------------------------------------------------
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<64ayxNJ3cDB@faerber.muc.de>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: claus@faerber.muc.de (Claus A. Faerber) Newsgroups: comp.std.c++ Subject: Re: delete or delete[] ??? Date: 10 Mar 1996 16:42:35 PST Organization: - Approved: austern@isolde.mti.sgi.com Message-ID: <64ayxNJ3cDB@faerber.muc.de> NNTP-Posting-Host: isolde.mti.sgi.com Keywords: delete X-Original-Date: Sun, 10 Mar 1996 01:00:00 +0100 In-Reply-To: <4hnqvf$mgr@fsgi01.fnal.gov> X-Mailer: CrossPoint v3.1 R/B17071 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMUN3GUy4NqrwXLNJAQEuPgIAid8cFpftvI32vNUnkHeKcwDt1TiaFBeT VMyNQmFqm7juHMs96v75MaT8MsjCsZLECDgSbhCgd7lwV4MZtTEm9w== =dDpH Originator: austern@isolde.mti.sgi.com