...
Code Block | ||||
---|---|---|---|---|
| ||||
#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 | ||||
---|---|---|---|---|
| ||||
#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);
// ...
} |
...