how to count the number of objects created in c++

前端 未结 6 1452
一个人的身影
一个人的身影 2020-12-13 21:14

how to count the number of objects created in c++

pls explain with a simple example

6条回答
  •  [愿得一人]
    2020-12-13 22:12

    You have to overload the new and delete operators to count memory allocations.

    void * operator new (size_t size)
    {
        void * p = malloc (size);
        num_allocations++;
        return p;
    }
    
    void operator delete (void * p)
    {
        num_deletions++;
        free (p);
    }
    

提交回复
热议问题