Topic: delete (T*)0;
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/06/23 Raw View
"Dave Abrahams" <abrahams@mediaone.net> wrote:
: I'm trying to find out whether, when calling delete on a null pointer value,
: the implementation is allowed to call operator delete anyway. I guess that
: it is legal for a function which is required to have "no effects" to call
: another function which has no effects, and operator delete is not allowed to
: have effects when called with a null pointer, so the answer is yes.
: Just how strongly is "no effect" indended?
I think that the last time this came up the answer was, "Does it make
any difference?"
Note that 5.3.5 does contain the paragraph
The delete expresion will call a deallocation function.
"Will" is not the magic word "shall".
It says that it "may" call a destructor. It better not for a nul
pointer; however, it makes no difference if it calls an operator
delete, since ::operator delete((T*)0) must be valid.
Could a class operator delete record the number of times it was
called with a nul pointer in a static object and the static object's
destructor produce an output?
I defer to others your use of "calling delete" as being the same
as writing a "delete expression." I don't see it that way, but
there is much I may misunderstand. The standard does not use
new expression operator, but it does use delete expression operator.
I don't think a delete expression "calls" a delete operator function.
John
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/06/23 Raw View
On 21 Jun 99 23:06:31 GMT, Dave Abrahams <abrahams@mediaone.net> wrote:
>I'm trying to find out whether, when calling delete on a null pointer value,
>the implementation is allowed to call operator delete anyway. I guess that
>it is legal for a function which is required to have "no effects" to call
>another function which has no effects, and operator delete is not allowed to
>have effects when called with a null pointer, so the answer is yes.
>
>Just how strongly is "no effect" indended?
What is your motivation for asking this question?
I was thinking that operator delete has to be called because one may
write code like this:
void * operator new(size_t N) {
logfile << "new " << N << '\n';
return std::malloc(N);
}
void operator delete(void * ptr) {
logfile << "delete " << ptr << '\n';
std::free(ptr);
}
void f(int * i) { delete i; }
int main() {
f(new int(0));
delete (void*)0;
}
I'd expect the contents of logfile to be
new 4
delete 0x0100
delete 0x00
In other words, the program must call operator delete. If operator
delete did not write to logfile, then it has no side effects --
ignoring the side effects in std::free -- and the logfile may have
just two lines
new 4
delete 0x0100
The compiler has discarded the call "delete (void*)0" as an
optimization.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James.Kanze@dresdner-bank.com
Date: 1999/06/23 Raw View
In article <oN6b3.1735$ec.32152@ndnws01.ne.mediaone.net>,
"Dave Abrahams" <abrahams@mediaone.net> wrote:
> I'm trying to find out whether, when calling delete on a null pointer
value,
> the implementation is allowed to call operator delete anyway.
Presumably, "effects" is something similar to "observable behavior",
since the compiler can do absolutely anything that doesn't change
observable behavior.
On the other hand, is the required behavior for operator delete (in
18.4.1.1.) exhaustive? Or is it legal for operator delete to have side
effects other than the required behavior? (This is an interesting
question. My own operator delete has the observable side effect of
removing the block from a list of allocated blocks; there is also a
function to dump the list.)
--
James Kanze mailto:
James.Kanze@dresdner-bank.com
Conseils en informatique orient=E9e objet/
Beratung in objekt orientierter
Datenverarbeitung
Ziegelh=FCttenweg 17a, 60598 Frankfurt, Germany Tel. +49 (069) 63 19 86
27
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/06/23 Raw View
Dave Abrahams wrote:
> I'm trying to find out whether, when calling delete on a null pointer
> value, the implementation is allowed to call operator delete anyway.
> I guess that it is legal for a function which is required to have
> "no effects" to call another function which has no effects, and
> operator delete is not allowed to have effects when called with a
> null pointer, so the answer is yes.
>
> Just how strongly is "no effect" indended?
I've always been under the impression that the following should
always work:
void foo()
{
int * p = new int;
delete p;
p = 0;
delete p; // Guaranteed to work, no effect
}
Since this code calls the global '::operator delete()', I am led
to the conclusion that calling delete on a null pointer will always
be safe. In other words, it is okay to go ahead and call delete on
a null pointer.
On the other hand, it appears to be unspecified whether the global
'::operator delete()' function is actually called if the delete
operand is null; it would seem to be reasonable to allow the compiler
to generate code to check for null in order to avoid making the
call. One way to find out would be to supply your own global
delete() and see what happens. In any case, it's a bad idea not to
check for null in user-written delete() functions; failing to do so
would appear to violate the guarantee.
-- David R. Tribble, dtribble@technologist.com --
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Dave Abrahams" <abrahams@mediaone.net>
Date: 1999/06/21 Raw View
I'm trying to find out whether, when calling delete on a null pointer value,
the implementation is allowed to call operator delete anyway. I guess that
it is legal for a function which is required to have "no effects" to call
another function which has no effects, and operator delete is not allowed to
have effects when called with a null pointer, so the answer is yes.
Just how strongly is "no effect" indended?
----
3.7.3.2 Deallocation functions [basic.stc.dynamic.deallocation]
3 The value of the first argument supplied to one of the deallocation
functions provided in the standard library may be a null pointer
value; if so, the call to the deallocation function has no effect.
Otherwise, the value supplied to operator delete(void*) in the stan-
dard library shall be one of the values returned by a previous invoca-
tion of either operator new(size_t) or operator new(size_t, const
std::nothrow_t&) in the standard library, and the value supplied to
operator delete[](void*) in the standard library shall be one of the
values returned by a previous invocation of either operator
new[](size_t) or operator new[](size_t, const std::nothrow_t&) in the
standard library.
5.3.5 Delete [expr.delete]
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...
---
[ 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://reality.sgi.com/austern_mti/std-c++/faq.html ]