计算在堆栈和堆中为多个类创建的对象

时间:2010-06-09 10:27:38

标签: c++

计算不同类的堆栈和堆中创建的对象总数的最佳方法是什么。我知道在C ++中,new和delete运算符可以重载,因此在默认构造函数和析构函数中,对象计数可以在对象被创建或销毁时递增或递减。

此外,如果我要为不同类的对象的对象计数扩展相同的东西,那么我可以创建一个虚拟类并在该类中编写对象计数代码,然后当我创建任何新类时,我可以从假人班。

是否存在针对同一问题的其他最佳解决方案。

3 个答案:

答案 0 :(得分:4)

  

然后我可以创建一个虚拟类并在该类中编写对象计数代码,然后当我创建任何新类时,我可以从Dummy类派生它。

是的,但由于每个类都需要自己的计数,因此您必须制作基类模板并使用奇怪的重复模板模式(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;
}

答案 1 :(得分:0)

在堆栈和堆中创建对象的方式不同。 使用new运算符在堆中创建一个对象,普通的对象声明将在堆栈中创建一个对象。 因此,通过重载 new 运算符,我们可以计算在堆中创建的对象。

静态变量可用于计数对象。

答案 2 :(得分:-2)

查找在堆和堆栈上创建的对象数。为了在堆上创建对象,我们使用new,所以要覆盖new和delete关键字以跟踪计数器。

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
}
相关问题