Skip to content

Instantly share code, notes, and snippets.

@kalman5
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save kalman5/f503e9e8be898e5483ad to your computer and use it in GitHub Desktop.

Select an option

Save kalman5/f503e9e8be898e5483ad to your computer and use it in GitHub Desktop.
Example set_new_handler
#include <iostream>
class ReservedMemory {
public:
ReservedMemory()
: theMemoryReserved(new char[80000*1024])
{}
~ReservedMemory() { delete []theMemoryReserved; }
void release() {
if (theMemoryReserved) {
std::cout << "FREEING SOME MEMORY" << std::endl;
delete []theMemoryReserved;
theMemoryReserved = nullptr;
} else {
std::cout << "NO MORE MEMORY TO FREE" << std::endl;
throw std::bad_alloc();
}
}
private:
const char* theMemoryReserved;
};
ReservedMemory rm;
int main(int argc, char** argv) {
std::set_new_handler([](){ rm.release(); });
char * ptr = new char[50000*1024];
std::cout << "SUCCEEDED" << std::endl;
char * ptra = new char[50000*1024];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment