Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: clarified result of reclaim_resources()

...

Code Block
bgColor#FFcccc
langcpp
#include <new>
 
void custom_new_handler() {
  // Returns number of bytes freed
  extern std::size_t reclaim_resources();
  reclaim_resources();
}
 
int main() {
  std::set_new_handler(custom_new_handler);
 
  // ...
}

...

In this compliant solution, custom_new_handler() does not disregard the return value from reclaim_resources(), which is returning the number of bytes freed by the reclamation. If it returns 0, then an exception of type std::bad_alloc is thrown, meeting the requirements for the replacement handler.

Code Block
bgColor#ccccff
langcpp
#include <new>

void custom_new_handler() noexcept(false) {
  // Returns number of bytes freed
  extern std::size_t reclaim_resources();
  if (0 == reclaim_resources()) {
    throw std::bad_alloc();
  }
}
 
int main() {
  std::set_new_handler(custom_new_handler);
 
  // ...
}

...