[comp.lang.c++] proper behavior of operator new

ttwang@polyslo.CalPoly.EDU (Thomas Wang) (09/04/89)

When memory on the free store runs out, operator 'new' will call the function
pointed by _new_handler.  When _new_handler finishes, I think the new
operator should try another time to allocate the storage.  This is because
the _new_handler may have deleted some unused storage space, so now there
is enough space after all.

On page 93 of "The C++ Programming Language", Bjarne suggested we re-define
the 'new' operator.  I don't think this is practical.  Re-defining the
'new' operator is difficult, and non-portable across systems.

I raised this issue, because the _new_handler can be used to do garbage
collection.  Right now, even after garbage collection, the new operator
will still return a null pointer.

 -Thomas Wang ("I am, therefore I am."
                 - Akira               )

                                                     ttwang@polyslo.calpoly.edu

ttwang@polyslo.CalPoly.EDU (Thomas Wang) (09/05/89)

ttwang@polyslo.CalPoly.EDU (Thomas Wang) writes:
>When memory on the free store runs out, operator 'new' will call the function
>pointed by _new_handler.  When _new_handler finishes, I think the new
>operator should try another time to allocate the storage.  This is because
>the _new_handler may have deleted some unused storage space, so now there
>is enough space after all.

I just did some testing.  The behavior of 'new' is different for cfront 1.2
and g++ 1.34.  Cfront will contineously call the _new_handler until some
memory is freed.  You can set_new_handler(0); to terminate the loop.  If
enough memory is freed, the object will be allocated with no problem.

On the other hand, g++ 1.34 will call _new_handler only once.  A null
pointer is returned even if enough memory is freed.  This looks like
a bug.

 -Thomas Wang ("I am, therefore I am."
                 - Akira               )

                                                     ttwang@polyslo.calpoly.edu