I'm using the RTL as a dynamic library. This allows for an overridden new/delete operator in global namespace, because the linker would find my implementation first. I could rely on malloc() and free() to do the allocation stuff, but there are things like 'new_handlers' and 'std::nothrow' objects. Standard library templates require a certain behaviour of the new/delete operators, but the standard library's implementation of new/delete in global namespace is out of scope! Is there an implementation in another namespace from the standard libary that I can use?
I might dynamically determine the address of the standard library's impementation (in the dll) and call it through a pointer, but that's not subject to a beauty reward. Still would this work? (I am using borland C++ builder 2006).
EDIT I want to ask this question:
Does this sample code behave similar to RTL's operator new(size_t)? Or did I missunderstand it?
void *new_replacement(size_t p_size)
{
void *l_retval = NULL;
while(l_retval == NULL && p_size != 0)
{
l_retval = malloc(p_size);
if (l_retval == NULL)
{
void (*l_handler)() = std::set_new_handler(NULL);
// could not find another way to obtain the current new_handler
if (l_handler == NULL) throw std::bad_alloc();
std::set_new_handler(l_handler);
l_handler();
}
}
return l_retval;
}