#include <iostream>
#include <cstdint>
using namespace std;
void* operator new( size_t size )
{
std::cout << "The size 'new' is actually asking for: " << size << " bytes." << std::endl;
void* memory = malloc( size ); // Let's say the pointer was 4
std::cout << "Original address: " << memory << std::endl;
return memory; // Pointer is still 4
}
class Object
{
public:
Object() = default;
virtual ~Object() = default; //Virtual, to give it a vtable, to ensure it's non-POD.
int meow = 357;
};
struct POD
{
uint64_t A;
uint64_t B;
};
int main()
{
const int NumObjectsToAllocate = 4;
std::cout << "Sizeof 'Object': " << sizeof(Object) << std::endl;
std::cout << "Allocating " << NumObjectsToAllocate
<< " Objects should take " << (NumObjectsToAllocate * sizeof(Object))
<< " bytes." << std::endl;
Object *stuff = new Object[ NumObjectsToAllocate ];
std::cout << "Resulting address: " << stuff << std::endl;
delete[] stuff;
std::cout << "---------------------------------------" << std::endl;
const int NumOfFloatsToAllocate = 4;
std::cout << "Sizeof 'float': " << sizeof(float) << std::endl;
std::cout << "Allocating " << NumOfFloatsToAllocate
<< " floats should take " << (NumOfFloatsToAllocate * sizeof(float))
<< " bytes." << std::endl;
float *floats = new float[ NumOfFloatsToAllocate ];
std::cout << "Resulting address: " << floats << std::endl;
delete[] floats;
std::cout << "---------------------------------------" << std::endl;
const int NumOfPodsToAllocate = 4;
std::cout << "Sizeof 'Pod': " << sizeof(POD) << std::endl;
std::cout << "Allocating " << NumOfPodsToAllocate
<< " pods should take " << (NumOfPodsToAllocate * sizeof(POD))
<< " bytes." << std::endl;
POD *pods = new POD[ NumOfPodsToAllocate ];
std::cout << "Resulting address: " << pods << std::endl;
delete[] pods;
return 0;
}