Counting of objects created in stack and heap for many classes
What is the best way to count the total number of objects created in both stack and heap for different classes. I know that in C++ new and delete operators can be overloaded and hence in the default constructor and destructor the object count can be incremented or decremented as and when the objects get created or destroyed.
Further if i am to extend the same thing for object counting of objects of different classes, then i can create a dummy class and write 开发者_如何学Pythonthe object count code in that class and then when i create any new class i can derive it from the Dummy class.
Is there any other optimal solution to the same problem.
then i can create a dummy class and write the object count code in that class and then when i create any new class i can derive it from the Dummy class.
Yes, but since each class needs its own count, you have to make a base class template and use the curiously recurring template pattern (CRTP):
template <class Derived>
class InstanceCounter
{
static int count;
protected:
InstanceCounter()
{
++count;
}
~InstanceCounter()
{
--count;
}
public:
static int instance_count()
{
return count;
}
};
template <class Derived>
int InstanceCounter<Derived>::count = 0;
class Test : public InstanceCounter<Test>
{
};
int main()
{
Test x;
std::cout << Test::instance_count() << std::endl;
{
Test y;
Test z;
std::cout << Test::instance_count() << std::endl;
}
std::cout << Test::instance_count() << std::endl;
}
The way the object is created in stack and heap is different. The new operator is used create an object in heap, and normal object declaration will create an object in stack. So by overloading an new operator we can count the object created in heap.
static variable can be used to count the objects.
To find the no of objects created on Heap and Stack. To create the object on the heap we use new so override the new and delete keywords to keep track of the counter.
class Test
{
static int heapcount;
static int stackcount;
public:
static int GetHeapCount()
{
return heapcount;
}
static int GetStackCount()
{
//The objects which are created on heap also will call constructor and increment the stack count, so remove the objects that are created on heap to get stack count
return stackcount - heapcount;
}
Test()
{
++stackcount;
}
~Test()
{
--stackcount;
}
void* operator new(size_t size)
{
++heapcount;
void * p = malloc(sizeof(Test));
return p;
}
void operator delete(void* p)
{
--heapcount;
free(p);
}
void operator=(Test const& obj)
{
int x = 0;
}
};
int Test::heapcount = 0;
int Test::stackcount = 0;
int main()
{
{
Test t;
Test t2;
Test* t1 = new Test();
std::cout << "HeapCount = " << Test::GetHeapCount() << std::endl;//HeapCount = 1 (t1)
std::cout << "StackCount = " << Test::GetStackCount() << std::endl;//StackCount = 2 (t,t2)
delete t1;
}
//As all the objects are deleted so count must be zero, stack objects will be removed when goes out of scope
std::cout << "HeapCount = " << Test::GetHeapCount() << std::endl;//HeapCount = 0
std::cout << "StackCount = " << Test::GetStackCount() << std::endl;//StackCount=0
{
Test t[3];
Test* t2 = new Test();
Test* t3 = new Test();
std::cout << "HeapCount = " << Test::GetHeapCount() << std::endl;//HeapCount = 2 (t2,t3)
std::cout << "StackCount = " << Test::GetStackCount() << std::endl;//StackCount = 3 (t[3])
}
//Two heap objects are not deleted, but stack objects has been cleaned
std::cout << "HeapCount = " << Test::GetHeapCount() << std::endl;//HeapCount = 2
std::cout << "StackCount = " << Test::GetStackCount() << std::endl;//StackCount = 0
}
精彩评论