bad_alloc1.cpp 435 B

1234567891011121314151617
  1. /*
  2. ulimit -S -v 204800
  3. g++ -g -O0 bad_alloc1.cpp -o bad_alloc && ./bad_alloc
  4. */
  5. #include <stdio.h>
  6. #include <new>
  7. void handler() {
  8. printf("Memory allocate failed\n");
  9. std::set_new_handler(NULL); // New will try to alloc again, then abort.
  10. }
  11. int main(){
  12. std::set_new_handler(handler);
  13. char* p1 = new char[193000 * 1024]; // huge allocation
  14. char* p0 = new char[100 * 1024]; // small allocation
  15. printf("OK\n");
  16. }