Topic: effect of delete null pointer
Author: Al Grant <algrant@myrealbox.com>
Date: Sun, 28 Feb 2010 20:05:44 CST Raw View
int x;
void operator delete(void*p) { x = 1; }
void f() { int *p = 0; delete p; }
Is an implementation (a) required to, (b) required
not to, (c) not required to, call operator delete?
5.3.5#2 states "if the value of the operand of delete
is the null pointer the operation has no effect".
(3.7.3.2#3 states that the deallocation function in
the standard library has no effect if given a null
pointer.)
Is there a requirement that a user-written deallocation
function also has no effect when given a null pointer
(in which case it doesn't matter whether delete invokes
it, as it would be unobservable)? Or is delete required
to meet the requirement of 5.3.5#2 by testing for null
before invoking the deallocation function?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =3D?ISO-8859-1?Q?Daniel_Kr=3DFCgler?=3D <daniel.kruegler@googlemail.c=.om>
Date: Mon, 1 Mar 2010 12:05:06 CST Raw View
On Mar 1, 3:05 am, Al Grant <algr...@myrealbox.com> wrote:
> int x;
> void operator delete(void*p) { x = 1; }
> void f() { int *p = 0; delete p; }
>
> Is an implementation (a) required to, (b) required
> not to, (c) not required to, call operator delete?
>
> 5.3.5#2 states "if the value of the operand of delete
> is the null pointer the operation has no effect".
> (3.7.3.2#3 states that the deallocation function in
> the standard library has no effect if given a null
> pointer.)
>
> Is there a requirement that a user-written deallocation
> function also has no effect when given a null pointer
> (in which case it doesn't matter whether delete invokes
> it, as it would be unobservable)? Or is delete required
> to meet the requirement of 5.3.5#2 by testing for null
> before invoking the deallocation function?
In C++03 the requirements are insufficiently ruled, see
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#348
The defect has been resolved for C++0x to require that
any deallocation function has to cope with a null
argument, see [expr.delete]/7:
"If the value of the operand of the delete-expression is
not a null pointer value, the delete-expression will call
a deallocation function (3.7.4.2). Otherwise, it is
unspecified whether the deallocation function will be
called."
so bullet (c) of your assumptions is correct.
HTH & Greetings from Bremen,
Daniel Kr=FCgler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]