🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #3636

Re: delete or delete[] ???

#3636
From: Fergus Henderson
Date: Mon, 11 Mar 1996 03:20
55 lines
2503 bytes
claus@faerber.muc.de (Claus A. Faerber) writes:

>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.

The fact that a typedef is used is irrelevant.

>It is an array of array_types, sou you have to use delete[].

Correct, because it is an array, 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.

No, that's incorrect.  It is, as you say, "one single array_type", but
`array_type' is an array type, so `delete[]' must be used.
See 5.3.5[expr.delete]/2.

>> delete   a2; // or should it be delete[] a2;
>
>Ok, points to a single array_type.

No, that's incorrect, for the same reason as above.

>> delete[] b2; // or should it be delete   b2;
>
>*No*, points also to a single array_type, so you actually  
>have to use delete b2.

Again, that's incorrect, for the same reason as above.

--
Fergus Henderson             	WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au              	PGP: finger fjh@128.250.37.3
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <4i062e$ji@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
References: <64ayxNJ3cDB@faerber.muc.de>

Show Full Headers
Complete Article Headers
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Message-ID: <4i062e$ji@mulga.cs.mu.OZ.AU>
X-Original-Date: 11 Mar 1996 03:18:38 GMT
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back
Date: 11 Mar 96 03:20:25 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: delete or delete[] ???
Organization: Comp Sci, University of Melbourne
References: <64ayxNJ3cDB@faerber.muc.de>
Keywords: delete
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMUOcCOEDnX0m9pzZAQH1VgF/R0nLy3hfURI+fYKIOkdWCGtZpFWROHFE
 5zs/SQSs8xKyuSroi0FWrItAzJkmFd7H
 =o1UN