- Joined
- Jun 23, 2008
- Messages
- 2
- Reaction score
- 0
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.
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
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:
#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