Consider the following code which contains a class with overloads for new
and new[]
:
#include <iostream>
class my_class {
public:
my_class() {};
my_class(const my_class& rhs) {};
~my_class() {};
//the constructor also as applicable.
void * operator new(size_t sz);
void * operator new[](size_t sz);
void operator delete(void * ptr);
void operator delete[](void * ptr);
private:
};
void * my_class::operator new(size_t sz)
{
std::cout << "at operator new, sz is: " << sz << std::endl;
}
void * my_class::operator new[](size_t sz)
{
std::cout << "at operator new[], sz is: " << sz << std::endl;
}
void my_class::operator delete(void * ptr)
{
std::cout << "at operator delete, ptr is: " << ptr << std::endl;
}
void my_class::operator delete[](void * ptr)
{
std::cout << "at operator delete[], ptr is: " << ptr << std::endl;
}
int main(int argc, char ** argv)
{
my_class * ptr = new my_class;
my_class * arr_ptr = new my_class[1];
return 0;
}
Running this in the terminal (Cygwin) I see:
at operator new, sz is: 1
at operator new[], sz is: 9
Why is sz == 9
when operator new[]
is called ? Who sets it? What does it depend on (eg. page size?)?