1

I have written below program to count the number of static and dynamically created object of the class.

#include <iostream>
class Test 
{
   public:
    Test()
    {
      stackCount++;
    }
    
    void* operator new(size_t objSize)
    {
       void* ptr = malloc(objSize);
       heapCount++;
       return ptr;
    }
    
    static void Display()
    {
      std::cout << "stack object count : " << stackCount - heapCount << ", Heap object Count :" <<  
      heapCount << std::endl;
    }
    
   private:
    static int stackCount;
    static int heapCount;
};
    
int Test::stackCount = 0;
int Test::heapCount = 0;
     
int main() 
{
    Test obj1;
    Test obj2;
    Test obj3;
       
    Test *ptr = new Test();
        
    Test::Display();
        
    return 0;
}

Program output : stack object count : 3, Heap object Count :1

is there any better way to maintain the count of static and dynamic object.?

  • 1
    So, your program works as expected? What are the criteria for "better" then? – The Dreams Wind Jul 20 '22 at 16:01
  • You are not deleting the counts as the items are deleted or go out of scope. – cup Jul 20 '22 at 16:03
  • 1
    Whatever you are trying to do (I cannot imagine any use for this) your custom new operator will not create valid object instances, you will need to call an in place new (https://stackoverflow.com/questions/3763846/what-is-an-in-place-constructor-in-c). – Pepijn Kramer Jul 20 '22 at 16:10
  • I think you'd also need a custom delete to match your new. Then there is copy construction and arrays (new[]) and there is an itch in the back of my neck that something else is being overlooked. – Avi Berger Jul 20 '22 at 16:18
  • 1
    `void* ptr = malloc(objSize);` -- You are not checking for `malloc` returning NULL. Also, as others have mentioned, your code will not cover the cases such as `new Test[5];`. There are probably other edge cases where this code will fail to do what you are claiming you want it to do. I also don't see a use for this, unless you are a C++ tool author that is writing a utility to track dynamic allocations. But even then, you would know to look out for these issues without being told about them. – PaulMcKenzie Jul 20 '22 at 16:24
  • @cup we want to maintain the count of total number of created object so if we will decrement the count when object will go out of scope then we will not get to know how many objects had been created. – Naveen Gupta Jul 20 '22 at 17:58
  • @TheDreamsWind my intention was to get the idea if we can implement this program with different logic instead of overloading new operator. – Naveen Gupta Jul 20 '22 at 18:00
  • If you actually implement the new method properly, won't the constructor get called? And so won't stackCount count all the objects, not just the ones on the stack? – Joseph Larson Jul 20 '22 at 18:34
  • @JosephLarson Correct, but if that were the only issue, there is an easy fix. Op's stackCount becomes totalCount and alleged actual stack count is total - heap – Avi Berger Jul 20 '22 at 18:55
  • 1
    Hmm. Time to scratch that back of the neck itch. What about std::vector? How does it get its heap memory to store objects?? And when std::vector reallocates & you get "extra" copy or move constructor calls? You get spurious counts of some kind no matter what. And in general, when do move constructions count? I don't think the "overloaded new" approach is going to fly. – Avi Berger Jul 20 '22 at 19:13

0 Answers0