🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Claus A. Faerber Mon, 11 Mar 1996 00:42
Re: delete or delete[] ???
#3638
Author: Claus A. Faerber
Date: Mon, 11 Mar 1996 00:42
50 lines
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 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads