Skip to content

Instantly share code, notes, and snippets.

@lucasdemarchi
Created August 3, 2017 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasdemarchi/fc3b9350e7214027f444cdff3f3ad917 to your computer and use it in GitHub Desktop.
Save lucasdemarchi/fc3b9350e7214027f444cdff3f3ad917 to your computer and use it in GitHub Desktop.
#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