Re: Customize operator new / delete
On May 11, 8:31 am, "io_x" <a...@b.c.invalid> wrote:
"James Kanze" <james.ka...@gmail.com> ha scritto nel messaggionews:bb132999-0899-4e03-b5b8-942ef18d079d@a10g2000vbz.googlegroups.com...
On May 8, 6:05 pm, "io_x" <a...@b.c.invalid> wrote:
"James Kanze" <james.ka...@gmail.com> ha scritto nel
messaggionews:78a4d447-9756-4dbc-ac6d-3513dbcd5031@l6g2000vbn.googlegroups.com...
The operator new do not meet the requirements set down in the
standard, particularly the post-condition that the returned
pointer is never null. You have to do something like:
void operator new( size_t size )
{
void* result = malloc( size );
if ( result == NULL ) {
throw std::bad_alloc();
for me better what does malloc(); it return 0, so each function know
the path they have to follow: the error path; each function for its
responsability.
I'd argue about that (although most of the applications I've
worked on have set the new handler to abort). Still, if you're
providing replacement for the standard operator new, you don't
have a choice. One of the post-conditions that must be met is
that you return a valid non-null pointer. And a lot of code
(e.g. in the standard library) counts on it, and will fail
otherwise.
but the C++ libraries are linked with the new, delete of C++,
right?
It depends. The standard requires that the implementation
provide some means of replacing the standard operators new and
delete. How the implementation does it (and what additional
commands it might require) is up to it. If the C++ libraries
are really libraries (and not fully linked DLL's), then this
will typically be trivial; separate object files in a library
are not yet linked, and the object file containing the standard
operators new and delete will only be linked into the program if
they resolve an otherwise unresolved external.
there is a conflict for some other new, delete, defined in some
other place?
The .asm result seems to indicate there is a name conflict
because the name of new[] operator is always "@$bnwa$qui" for both
[std new[] and defined new[] ];
but the .exe result show there is no conflict because the real name of the
new, delete of the standard C++ in this compiler is different; something
as __org_.. show in the debugger different from "operator new"
show in the debugger
so locally here it seems, there is no problem and the two function
new[] are recognise different; ok from the sys and for what i think
should be right
operator new() and operator new[]() are two different functions,
with two different names. (What the standard does require is
that the standard operator new[] call operator new() to obtain
its memory.)
--
James Kanze