Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <new>
- #include <iomanip>
- #include <malloc.h>
- #ifdef _DEBUG
- void* _perform_alloc(std::size_t count)
- {
- std::cout << "[DEBUG] Allocating "
- << std::dec << count
- << " bytes of heap memory" << std::endl;
- void *ptr = malloc(count);
- if (!ptr) {
- std::cerr << "[DEBUG] Allocation failed" << std::endl;
- throw std::bad_alloc();
- }
- std::cout << "[DEBUG] Allocated at "
- << std::showbase << std::hex << ptr
- << std::endl;
- return ptr;
- }
- void* operator new(std::size_t count)
- {
- return _perform_alloc(count);
- }
- void* operator new[](std::size_t count)
- {
- return _perform_alloc(count);
- }
- #endif
- int main()
- {
- int *t1 = new int;
- int *t2 = new int[20];
- double *t3 = new double[2000000000]; // This really should fail
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement