This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: malloc/free & new/delete balance
- To: carlo at runaway dot xs4all dot nl, oliva at dcc dot unicamp dot br
- Subject: Re: malloc/free & new/delete balance
- From: mrs at wrs dot com (Mike Stump)
- Date: Wed, 1 Jul 1998 18:17:03 -0700
- Cc: egcs at cygnus dot com
> To: Carlo Wood <carlo@runaway.xs4all.nl>
> Cc: egcs@cygnus.com (egcs@cygnus.com)
> From: Alexandre Oliva <oliva@dcc.unicamp.br>
> Date: 29 Jun 1998 21:54:36 -0300
> > This unbalance between calling `::operator new(size_t)' and
> > `free(void *)' is causing troubles when `operator new(size_t)' and
> > `operator delete(void *)' are overloaded
> Agreed, but, for some reason, the FDIS [lib.new.delete.array]
> defines operator new[](size_t) as returning operator new(size_t) by
> default, while it states that operator delete[](void*) frees
Can you provide an exact quote of this? I can't find it. I'll quote
from the FDIS below.
> the pointer it is given. So, I'd say the implementation is correct,
> and calling operator delete(void*) is non-standard. Too bad. :-(
The next stage of your learning is to be able to say when the standard
is wrong. :-)
Also, I think we are staring at different papers. Does yours have the
number 14882 or X3J16/97-0079 on it? If not, that isn't the FDIS, and
you should not call it that.
>From the real FDIS:
void operator delete[](void* ptr) throw();
void operator delete[](void* ptr, const std::nothrow_t&) throw();
-9- Effects: The deallocation function
(basic.stc.dynamic.deallocation) called by the array form of a
delete-expression to render the value of ptr invalid.
-10- Replaceable: a C++ program can define a function with this
function signature that displaces the default version defined by the
C++ Standard library.
-11- Required behavior: accept a value of ptr that is null or that was
returned by an earlier call to operator new[](std::size_t) or operator
new[](std::size_t,const std::nothrow_t&).
-12- Default behavior:
For a null value of ptr , does nothing.
Any other value of ptr shall be a value returned earlier by a
call to the default operator new[](std::size_t).*
[Footnote: The value must not have been invalidated by
an intervening call to operator delete[](void*)
(lib.res.on.arguments). --- end foonote]
For such a non-null value of ptr , reclaims storage allocated
by the earlier call to the default operator new[].
-13- It is unspecified under what conditions part or all of such
reclaimed storage is allocated by a subsequent call to operator new or
any of calloc, malloc, or realloc, declared in <cstdlib>.
Note the word above, ``reclaims.'' The calls should be symmetric as
one would guess without reading the standard. In fact, it is a bug
that the standard doesn't refine the language and say exactly that it
must call delete.