I am at a loss as to explain why, in the sample code, the call new Foo [4]
to the custom operator new[]
requests 68 bytes -- 4 more bytes than it ought to (sizeof(Foo) == 16
), whereas the more arcane call Foo::operator new[]( 4 * sizeof( Foo ) )
correctly requests 64 bytes. Note that when the member std::vector<long> m_dummy
is removed from Foo
both calls correctly request 16 bytes (code on ideone).
#include <vector>
#include <iostream>
struct MemoryManager
{
static void* allocate( unsigned size )
{
static char block[256];
return block;
}
};
class Foo
{
public:
void* operator new[]( size_t size )
{
std::cout << "operator new[] : data size -- " << size << std::endl;
return MemoryManager::allocate( size );
}
private:
std::vector<long> m_dummy; // Huh?
unsigned m_num;
};
int main( int argc, char * argv[] )
{
std::cout << "Foo size: " << sizeof( Foo ) << std::endl;
new Foo [4];
Foo::operator new[]( 4 * sizeof( Foo ) );
}