From: Carlo Wood <carlo@runaway.xs4all.nl>
To: egcs@cygnus.com (egcs@cygnus.com)
Subject: malloc/free & new/delete balance
Date: Mon, 29 Jun 1998 08:41:00 -0000 [thread overview]
Message-ID: <199806291141.NAA19053@jolan.ppro> (raw)
In gcc/cp/new2.cc,
WEAK(void * operator new[] (size_t sz) throw (std::bad_alloc))
{
return ::operator new(sz);
}
...
WEAK (void operator delete[] (void *ptr) throw ())
{
if (ptr)
free (ptr);
}
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 [ for example to call special memory allocation/deallocation
functions called malloc_with_prepended_magic_number(size_t) and
free_with_prepended_magic_number(void *). Then calling 'operator
new[](size_t)' uses malloc_with_prepended_magic_number(size_t) and
'operator delete[](void *)' uses just free(void *) ].
The way it is now you force people to also overload operator new[] and
operator delete[] when they overloaded operator new and operator delete.
Imho, the only correct way is to balance this and use:
WEAK (void operator delete[] (void *ptr) throw ())
{
::operator delete(ptr);
}
Alternatively, and probably a little faster, is to duplicate the
code from operator new(size_t) and use:
WEAK(void * operator new[] (size_t sz) throw (std::bad_alloc))
{
/*
* This is an exact copy of
* void * operator new (size_t sz) throw (std::bad_alloc)
*/
void *p;
/* malloc (0) is unpredictable; avoid it. */
if (sz == 0)
sz = 1;
p = (void *) malloc (sz);
while (p == 0)
{
new_handler handler = __new_handler;
if (! handler)
throw bad_alloc ();
handler ();
p = (void *) malloc (sz);
}
return p;
}
Comments?
--
Carlo Wood <carlo@runaway.xs4all.nl>
next reply other threads:[~1998-06-29 8:41 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-06-29 8:41 Carlo Wood [this message]
1998-06-30 1:02 ` Alexandre Oliva
1998-06-30 15:15 ` Carlo Wood
1998-06-30 14:46 ` Alexandre Oliva
1998-06-30 14:46 Sol Foster
1998-07-01 22:54 Mike Stump
1998-07-02 1:39 ` Alexandre Oliva
1998-07-03 7:20 Sol Foster
1998-07-03 19:30 ` Carlo Wood
[not found] <199807031202.IAA26419.cygnus.egcs@maniac.deathstar.org>
1998-07-05 19:46 ` Nathan Myers
1998-07-06 14:48 ` Joern Rennecke
1998-07-06 18:52 ` John Carr
1998-07-07 1:12 ` Joern Rennecke
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=199806291141.NAA19053@jolan.ppro \
--to=carlo@runaway.xs4all.nl \
--cc=egcs@cygnus.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).