#include "tracker.h" #include #include std::size_t allocations { 0 }; std::size_t max_allocations { 0 }; std::size_t total_allocations { 0 }; void* operator new(std::size_t nbytes) { // only create the tracker if operator new was called static Tracker instance { }; auto result = std::malloc(nbytes); if (result == nullptr) return result; ++allocations; ++total_allocations; if (allocations > max_allocations) max_allocations = allocations; return result; } void operator delete(void* memory) noexcept { std::free(memory); if (memory == nullptr) return; --allocations; } void operator delete(void* memory, std::size_t) noexcept { return operator delete(memory); } Tracker::~Tracker() { if (allocations != 0) { std::cout << "WARNING: " << allocations << " allocations were not deallocated" << std::endl; } std::cout << "In total " << total_allocations << " allocations" << std::endl; std::cout << "At most " << max_allocations << " allocations at the same time" << std::endl; }