Created
August 3, 2017 14:41
-
-
Save lucasdemarchi/fc3b9350e7214027f444cdff3f3ad917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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