This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: malloc/free & new/delete balance
- To: egcs at cygnus dot com
- Subject: Re: malloc/free & new/delete balance
- From: Sol Foster <colomon at ralf dot org>
- Date: Fri, 03 Jul 1998 08:02:46 -0400
Alexandre Oliva <oliva@dcc.unicamp.br> wrote:
> I didn't write delete[] calls free(), I just said it frees
> (deallocates) the memory. No where it says delete[] calls delete by
> default, and, since it does for new[] and new, I took it as a
> difference, rather than as an omission.
I'd argue it's strongly implied that, if the memory is allocated with new,
then it is deallocated with delete. It's disappointing that the standard
doesn't specify this, but then, it doesn't even specify that the memory is
actually given back to the heap. (At least, that's my reading, and
Plauger seems to agree: "It is unspecified under what conditions part or
all of such reclaimed storage is allocated by a subsequent call to
operator new(size_t), or to any of calloc(size_t), malloc(size_t),
or realloc(void*, size_t).")
Can anyone think of any reasons other than these to use free?
1) delete just calls free anyway, so this saves a function call.
2) The standard doesn't absolutely specify using delete, so standard
conforming programs must replace new[] and delete[] if they replace new
and delete. Therefore we should introduce hideous bugs into their code if
they don't comply.
Is the extra function call that much more expensive than what free does
internally?
--
Sol Foster: colomon@ralf.org
If you pick up a starving dog and make him prosperous, he will not
bite you. This is the principal difference between a dog and a man.
-Mark Twain