Rob said:
Gianni Mariani wrote in
operator delete doesn't take size info, demo below.
I think it does.
#include <iostream>
char allocator_data[1024];
int amount_allocated = 0;
class Foo
{
public:
void * operator new( unsigned int size )
{
char * ptr = ( allocator_data + amount_allocated );
amount_allocated += size;
std::cout << "Allocate size is " << size << std::endl;
return ( void * ) ptr;
}
void * operator new[]( unsigned int size )
{
char * ptr = ( allocator_data + amount_allocated );
amount_allocated += size;
std::cout << "Allocate size is " << size << std::endl;
return ( void * ) ptr;
}
void operator delete( void * ptr, unsigned int size )
{
std::cout << "delete size is " << size << std::endl;
// drop memory on the floor
}
void operator delete[] ( void * ptr, unsigned int size )
{
std::cout << "delete size is " << size << std::endl;
// drop memory on the floor
}
};
class B : public Foo
{
public:
int xxx;
int yyy;
};
int main()
{
B * z = new B;
B * k = new B[30];
delete z;
delete [] k;
}
Admittedly it's an optional parameter BUT it is available and I've seen
some really KULE allocators that use this information when managing
extreme numbers of tiny objects and saving big time on memory and cycles.
The type info in delete is (can) only be used to call the destructor.
Most of the time.
Also there would be little utility in banning delete (void *), as
all it really does is call the global operator delete( void * ). It
never needs the :: when used in a member function though.
delete ptr
and
operator delete( ptr )
are 2 different things.
delete ptr also calls the destructor (if it exists) while operator
delete( ptr ) simply calls the method "operator delete( ... )".