We want to work with a system with a low latency, heap allocation in the application is more expensive. but to some extent the creation of an object on the heap is allowed. That is why we want to indicate whether an object is created on the heap or not.?
Is the following methodology the correct way to detect an object created in heap memory.?
Will have a common class in which the new and remote operator is overloaded to support heaps highlighted by pointers ....
#include <iostream> #include <set> using namespace std; class MemStat //base class { typedef set<MemStat*> POINTERS; static POINTERS m_ptrlist; public: void* operator new (size_t size) { MemStat* ptr = ::new MemStat; m_ptrlist.insert(ptr); return ptr; } void operator delete(void* dptr) { MemStat* ptr = static_cast<MemStat*>(dptr); m_ptrlist.erase(ptr); ::delete ptr; } // void* operator new[] (size_t sz); // void operator delete[] (void*); bool is_on_heap() { m_ptrlist.find(this) != m_ptrlist.end(); } protected: // ctor & dtor are protected for restrictions MemStat() { } virtual ~MemStat() { } MemStat(const MemStat&) { } const MemStat& operator=(const MemStat& ) { return *this; } }; MemStat::POINTERS MemStat::m_ptrlist;
for the end-user classes that we need to check to create the heap, will be obtained from the MemStat class, use the new delete and delete call when creating the base class object.
class MyClass : public MemStat
source share