Topic: delete[]
Author: regis@info.unicaen.fr (Regis)
Date: Mon, 2 Dec 2002 16:34:18 +0000 (UTC) Raw View
Greetings,
I would understand the section of the draft pasted below as follows:
int * obj = new int; delete [] obj; // undefined behavior.
int * arr= new int [10]; delete arr; // undefined behavior.
However, I find myself unable to interpret the final note
between brackets:
[Note: this means that the syntax of the delete-expression
must match the type of the object allocated by new, not the syntax of
the new-expression. ]
Would somebody be kind enough to clarify and exemplify it for me?
--
Regis
-------------------------------------
5.3.5 Delete [expr.delete]
1 The delete-expression operator destroys a most derived object
(_intro.object_) or array created by a new-expression.
delete-expression:
::opt delete cast-expression
::opt delete [ ] cast-expression
The first alternative is for non-array objects, and the second is for
arrays. The operand shall have a pointer type, or a class type having
a single conversion function (_class.conv.fct_) to a pointer type.
The result has type void.
2 If the operand has a class type, the operand is converted to a pointer
type by calling the above-mentioned conversion function, and the con-
verted operand is used in place of the original operand for the
remainder of this section. In either alternative, if the value of the
operand of delete is the null pointer the operation has no effect. In
the first alternative (delete object), the value of the operand of
delete shall be a pointer to a non-array object created by a new-
expression, or a pointer to a sub-object (_intro.object_) representing
a base class of such an object (_class.derived_). If not, the behav-
ior is undefined. In the second alternative (delete array), the value
of the operand of delete shall be the pointer value which resulted
from a previous array new-expression.68) If not, the behavior is unde-
fined. [Note: this means that the syntax of the delete-expression
must match the type of the object allocated by new, not the syntax of
the new-expression. ]
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: hyrosen@mail.com (Hyman Rosen)
Date: Mon, 2 Dec 2002 19:12:52 +0000 (UTC) Raw View
Regis wrote:
> [Note: this means that the syntax of the delete-expression
> must match the type of the object allocated by new, not the syntax of
> the new-expression. ]
>
> Would somebody be kind enough to clarify and exemplify it for me?
typedef int i[10];
int *arr = new i;
delete [] arr;
Since the new expression allocated an array, the array form of delete
must be used to free it, even though the syntax of the new expression
is the non-array version.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]