My C++ program needs a block of uninitialized memory and a void*
pointer to that block so that I can give it to a third party library. I want to pass control of the block lifetime to the library, so I don't want to use std::vector
. When the library is done with the block it will call a callback that I have to supply and that will deallocate the block. In C I would use malloc()
and later free()
.
In C++ I can either call ::operator new
or ::operator new[]
and ::operator delete
or operator delete[]
respectively later:
void* newBlock = ::operator new( sizeOfBlock );
// then, later
::operator delete( newBlock );
Looks like both ::operator new
and ::operator new[]
have exactly the same signature and exactly the same behavior. The same for ::operator delete
and ::operator delete[]
. The only thing I shouldn't do is pairing operator new
with operator delete[]
and vice versa - undefined behavior. Other than that which pair do I choose and why?