fork download
  1. #include <iostream>
  2. #include <new>
  3.  
  4. int main()
  5. {
  6. try
  7. {
  8. while(true)
  9. {
  10. new int[100000000ul]; // throwing overload
  11. }
  12. }
  13. catch (const std::bad_alloc& e)
  14. {
  15. std::cout << e.what() << std::endl;
  16. }
  17.  
  18. while (true)
  19. {
  20. int* p = new(std::nothrow) int[100000000ul]; // non-throwing overload
  21. if (p == nullptr)
  22. {
  23. std::cout << "Allocation returned nullptr" << std::endl;
  24. break;
  25. }
  26. }
  27. }
Success #stdin #stdout 0s 785920KB
stdin
Standard input is empty
stdout
std::bad_alloc
Allocation returned nullptr