Topic: calling for interpretation of 5.3.5/2


Author: "B. van Ingen Schenau" <bvis@universalmail.com>
Date: Tue, 9 Apr 2002 16:53:41 GMT
Raw View
In alt.comp.lang.learn.c-c++ we are having a bit of a discussion about
calling delete[] on null pointers, when that null-pointer did not
result from a call to new[].
So, my question to this group is:

Is this legal:

int main() {
  int *p = 0;
  delete[] p;
}

and why is it legal or not legal?

Bart v Ingen Schenau

---
[ 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: "Stuart Golodetz" <sgolodetz@dial.pipex.com>
Date: Tue, 9 Apr 2002 18:33:45 GMT
Raw View
"B. van Ingen Schenau" <bvis@universalmail.com> wrote in message
news:nb56bucvpsahcrcfbfr96u8tl8mel658o2@4ax.com...
> In alt.comp.lang.learn.c-c++ we are having a bit of a discussion about
> calling delete[] on null pointers, when that null-pointer did not
> result from a call to new[].
> So, my question to this group is:
>
> Is this legal:
>
> int main() {
>   int *p = 0;
>   delete[] p;
> }
>
> and why is it legal or not legal?

5.3.5 Delete

The delete-expression operator destroys a most derived object (1.7) or array
created by a newexpression.

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 (12.3.2) 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 converted
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.***

HTH,

Stuart.

>
> Bart v Ingen Schenau
>
> ---
> [ 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                       ]
>


---
[ 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: Robert W Hand <rwhand@NOSPAMoperamail.com>
Date: Tue, 9 Apr 2002 21:21:37 GMT
Raw View
On Tue,  9 Apr 2002 16:53:41 GMT, "B. van Ingen Schenau"
<bvis@universalmail.com> wrote:


>  int *p = 0;
>  delete[] p;
>}
>
>and why is it legal or not legal?

Legal.  Subclause 5.3.5/2 has jurisdiction here.  It clearly states
that in "either alternative", i.e. ::opt delete cast-expression and
::opt delete[] cast_expression, if the value of the operand of delete
is the null pointer, the expression has no effect.  So it is legal.
Finis.

I would suspect that the confusing part for you is a subsequent
sentence in the same paragraph that suggests that the value of the
operand of delete array (the "second alternative") must be a pointer
value that resulted from a previous array new-expression.

There is further confusion.  A prior sentence in that paragraph does
not say that the first alternative needs an operand whose value is a
pointer value that resulted from a previous object new-expression.
The reference to subclause 1.8, the definition of an object -
including objects that are not allocated with new, is regrettable.

So I think that your question is clearly answered, but that there is
some confusion in subsequent sentences that should be cleared up.  I
am not aware that it is the subject of a defect report.  But it looks
like it should be.

Best wishes,

Bob

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