Problems in overloading operators new[] and delete[]

Discussion in 'C Programming' started by tharinda_g, Jun 24, 2008.

  1. tharinda_g

    tharinda_g

    Joined:
    Jun 23, 2008
    Messages:
    2
    Hi,
    I am trying to get a good picture on how the objects are created and destroyed in one of my projects. Therefore I wrote the following macros so that they can be easily used in anywhere.

    Code (Text):
    #include <unistd.h>
    #include <stdio.h>

    #define DECLARE_NEW \
        static void* operator new(size_t size);\
        static void  operator delete(void* p);  \
        static void* operator new[](size_t size);\
        static void  operator delete[](void* p);    \
        static long  __lAllocations;    \
        static long  __lDeallocations;

    #define IMPLEMENT_NEW(class_name) \
        long class_name::__lAllocations = 0;\
        long class_name::__lDeallocations = 0; \
        void* class_name::operator new(size_t size) \
            {++__lAllocations; return ::operator new(size);}    \
        void class_name::operator delete(void* p)   \
        {++__lDeallocations; return ::operator delete(p);}\
        void* class_name::operator new[](size_t size) \
        {__lAllocations += (size / sizeof(class_name)); return ::operator new[](size);} \
        void class_name::operator delete[](void* p) \
        {__lDeallocations += (sizeof((class_name*)p) / sizeof(class_name)); return ::operator delete[](p);}

    #define GET_ALLOC_STATS(class_name, pfnCallback)\
        {(*pfnCallback)(#class_name, class_name::__lAllocations, class_name::__lDeallocations);}


    void printmemstatus(const char* zClassName, long lAllocations, long lDeallocations)
    {
        char zBuff[101];
        snprintf(zBuff, 101, "%s : Allocations = %lu, De-Allocations = %lu", zClassName, lAllocations, lDeallocations);
        puts(zBuff);
    }

    class MyClass
    {
    public:
        MyClass()
        {
            puts(__PRETTY_FUNCTION__);
        }

        ~MyClass()
        {
            puts(__PRETTY_FUNCTION__);
        }

        DECLARE_NEW
    };

    IMPLEMENT_NEW(MyClass)

    int main()
    {
        MyClass *p =  new MyClass;
        GET_ALLOC_STATS(MyClass, printmemstatus)
       
        delete p;
        GET_ALLOC_STATS(MyClass, printmemstatus)

        MyClass *ap = new MyClass[5];
        GET_ALLOC_STATS(MyClass, printmemstatus)

        delete[] ap;
        GET_ALLOC_STATS(MyClass, printmemstatus)

        return 0;
    }

     
    The output is

    MyClass::MyClass()
    MyClass : Allocations = 1, De-Allocations = 0
    MyClass::~MyClass()
    MyClass : Allocations = 1, De-Allocations = 1
    MyClass::MyClass()
    MyClass::MyClass()
    MyClass::MyClass()
    MyClass::MyClass()
    MyClass::MyClass()
    MyClass : Allocations = 10, De-Allocations = 1
    MyClass::~MyClass()
    MyClass::~MyClass()
    MyClass::~MyClass()
    MyClass::~MyClass()
    MyClass::~MyClass()
    MyClass : Allocations = 10, De-Allocations = 5

    My question is that the new[] and delete[] operators are not working as I expected. Is there anyway that I can make them work or is it beyond my reach to track the allocations when creating arrays?

    Thanks!
    Tharinda
     
    tharinda_g, Jun 24, 2008
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Natalie
    Replies:
    0
    Views:
    449
    Ron Natalie
    Jul 9, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    490
    Alf P. Steinbach
    Aug 6, 2003
  3. Nimmi Srivastav

    Overloading new and delete operators

    Nimmi Srivastav, Jan 31, 2004, in forum: C++
    Replies:
    3
    Views:
    2,784
    John Harrison
    Feb 2, 2004
  4. Douglas Peterson
    Replies:
    1
    Views:
    2,906
    Larry Brasfield
    Jan 22, 2005
  5. rohits123@gmail.com

    Regarding overloading new and delete operators

    rohits123@gmail.com, Feb 1, 2007, in forum: C++
    Replies:
    2
    Views:
    396
    rohits123@gmail.com
    Feb 1, 2007
Loading...

Share This Page