Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
//#define NEW 1
#if NEW
namespace ap {
static void *operator_new(size_t size)
{
if (size < 1) {
size = 1;
}
printf("local new operator: calloc %zd\n", size);
return calloc(size, 1);
}
void operator_delete(void *p)
{
free(p);
}
}
class CallocAllocator {
public:
void *operator new(size_t sz) {
return ap::operator_new(sz);
}
void *operator new[](size_t sz) {
return ap::operator_new(sz);
}
void operator delete(void *p) {
ap::operator_delete(p);
}
void operator delete[](void *p) {
ap::operator_delete(p);
}
};
#else
class CallocAllocator {
};
void * operator new(size_t size)
{
if (size < 1) {
size = 1;
}
return(calloc(size, 1));
}
void operator delete(void *p)
{
if (p) free(p);
}
void * operator new[](size_t size)
{
if (size < 1) {
size = 1;
}
return(calloc(size, 1));
}
void operator delete[](void * ptr)
{
if (ptr) free(ptr);
}
#endif
class A : public CallocAllocator {
public:
A(uint32_t a) : a(a) { }
A() { }
uint32_t a;
};
int main(int argc, char *argv[])
{
A *a = new A();
if (a->a > 0) {
printf("Whaaat??\n");
}
delete a;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment