Overloading 'new' and 'delete' operators of a class to return 'new char[]', could it be harmful?

99 Views Asked by At

I've come across a piece of code that I find suspicious, and I've seen it extensively used in most of the classes of a project.

It's a new and delete overload like this:

void* MyObject::operator new ( size_t  size )
{
    return ( size == 0 ? NULL : new char[size] ) ;
}

void* MyObject::operator new[] ( size_t  size )
{
    return ( size == 0 ? NULL : new char[size] ) ;
}

void MyObject::operator delete( void *p )
{
    char* l_tmp = (char*)p;
    delete[] l_tmp;
}

void MyObject::operator delete[]( void *p )
{
    char* l_tmp = (char*)p;
    delete[] l_tmp;
}

Can this be harmful in any way or form regarding memory, speed, or stability?

Does it make any sense?

1

There are 1 best solutions below

4
elfprince13 On

In most standards compliant C++ settings, new should throw std::bad_alloc rather than return nullptr. It's unclear without a lot more context whether changing this behavior for a specific class is actually desirable in your specific instance, or if the code was just written by someone who hates exceptions; however, the fact that the original author wrote NULL rather than nullptr is circumstantial evidence marking it as "old school" in a way that makes me suspect the latter.