fork download
  1. #include <new>
  2. #include <iostream>
  3. #include <cstdlib>
  4.  
  5. struct allocator {};
  6.  
  7. void *operator new(std::size_t size, allocator&) {
  8. if(void *ptr = std::malloc(size)) {
  9. return ptr;
  10. }
  11. throw std::bad_alloc();
  12. }
  13.  
  14. void operator delete(void *ptr) {
  15. std::cout << "deleting\n";
  16. std::free(ptr);
  17. }
  18.  
  19. struct s {
  20. s() {
  21. throw "failed";
  22. }
  23. };
  24.  
  25. int main() {
  26. allocator a;
  27.  
  28. try {
  29. new (a) s;
  30. } catch(...) {
  31. std::cout << "catching\n";
  32. }
  33. }
  34.  
Success #stdin #stdout 0s 3060KB
stdin
Standard input is empty
stdout
catching