Topic: delete or delete[] ???


Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 1996/03/07
Raw View
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];
array_type* a2 = new array_type;
array_type* b2 = new some_type[10];
...
delete[] a1;
delete[] b1;
delete   a2; // or should it be delete[] a2;
delete[] b2; // or should it be delete   b2;
--
***** Listen Americans, the IRS is your taxer,  the IRS is one. *****
David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
Voice: 1 708 840 3942      Deparment Fax: 1 708 840 3785
---
[ 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
]





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/03/09
Raw View
b91926@fsgi01.fnal.gov (David Sachs) writes:

>Should delete or delete[] be used for a pointer to an array type?

`delete[]' should be used.

See 5.3.5[expr.delete]/2, which says (amoung other things)
that the syntax of the delete expression must match the *type* of
the corresponding new expression, not its *syntax*.

>// 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];
>array_type* a2 = new array_type;
>array_type* b2 = new some_type[10];
>...
>delete[] a1;
>delete[] b1;
>delete   a2; // or should it be delete[] a2;

Yes, it should be `delete[] a2;'.

>delete[] b2; // or should it be delete   b2;

Nope, that one is correct.

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





Author: claus@faerber.muc.de (Claus A. Faerber)
Date: 1996/03/10
Raw View
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
]