Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problems in overloading operators new[] and delete[]

Reply
Thread Tools

Problems in overloading operators new[] and delete[]

 
 
tharinda_g tharinda_g is offline
Junior Member
Join Date: Jun 2008
Posts: 2
 
      06-24-2008
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:
#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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding overloading new and delete operators rohits123@gmail.com C++ 2 02-01-2007 10:56 AM
Functions, Operators, and Overloading? Michael Yanowitz Python 6 07-24-2006 08:41 PM
problems with Overloading operators Yudan YI \(OSU\) C++ 4 02-11-2005 07:14 AM
Overloading class new and delete operators with parameters Douglas Peterson C++ 1 01-22-2005 08:33 PM
Overloading new and delete operators Nimmi Srivastav C++ 3 02-02-2004 07:20 AM



Advertisments