fork download
  1. #include <iostream>
  2.  
  3. const int size = 1024;
  4. char pool[size];
  5. int nextFree = 0;
  6.  
  7. class A
  8. {
  9. public:
  10. int i = 123; // some data
  11.  
  12. A() { std::cout << "constructor\n"; }
  13. ~A() { std::cout << "destructor\n"; }
  14.  
  15. static void* operator new(std::size_t size) noexcept
  16. {
  17. void *ptr = &pool[nextFree];
  18. nextFree += size;
  19. return ptr;
  20. }
  21.  
  22. static void operator delete(void* ptr, std::size_t size) noexcept
  23. {
  24. //memset(ptr, 0, size); for example
  25. }
  26. };
  27.  
  28. int main()
  29. {
  30. A* a = new A();
  31. std::cout << std::boolalpha << (a == reinterpret_cast<A*>(&pool[0])) << std::endl;
  32. delete a;
  33. }
Success #stdin #stdout 0s 4408KB
stdin
Standard input is empty
stdout
constructor
true
destructor