473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to replace delete[] with a macro

foo
I'm creating a debug class called debug_mem_allocation for the purpose
of finding memory leaks.
I used macro's to replace the new and delete operators.
My problem is with trying to replace the delete[] operator with a
macro.

I can't replace the delete[] operator by using void* as the first
parameter, because then my code will not be able to modify the calling
function's pointer, nor would it get the source file name and line
number.
My current work around has been to modify the source code by replacing
delete[] with DELETE_ARRAY_AND_NULLIFY, which is defined as delete[]
for NON-DEBUG compile, and defined as an operator-() of my debug class
for DEBUG compile.

Can anyone think of a better way to replace the delete[] operator via
macro that would not require me to modify the source code?

My compilers are VC++ 6.0 and GNU 3.x

Here's the header:

class debug_mem_allocation
{
public:
enum ACTION_SWITCH{AllocateMem, DeAllocMem, GenerateReport};
debug_mem_allocation(const char* FileName, int LineNo, const char*
FunctionName)
:m_FileName(FileName), m_LineNo(LineNo), m_FuncName(FunctionName){}
template<typename T>
void operator+(T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
ptr = NULL;
}
template<typename T>
void operator+(const T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
}
template<typename T>
void operator-(T &ptr)
{
Log(ptr, DeAllocMem);
delete [] ptr;
ptr = NULL;
}
template<typename T>
T& operator<<(T &ptr)
{
Log(ptr, AllocateMem);
return ptr;
}
template<typename T>
void free_and_nullify(T &memblock , const char* VariableName)
{
Log(memblock, DeAllocMem);
free(memblock);
memblock = NULL;
}
void *malloc_and_log(size_t size );
void *realloc_and_log(void *memblock, size_t size );
static void SendOutCurrentLog();
private:
void Log(void const * const Ptr, ACTION_SWITCH Sw);
const char* m_FileName;
const int m_LineNo;
const char* m_FuncName;
};
#if (defined(DEBUG) || defined (_DEBUG)) &&
defined(USE_DEBUG_MEM_ALLOCATION)
#define new debug_ext::debug_mem_allocation(__FILE__, __LINE__,
__FUNCTION__)<< new
#define delete debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__)+
#define DELETE_ARRAY_AND_NULLIFY debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__)-
#define malloc(x) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).malloc_and_log(x)
#define realloc(m,s) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).realloc_and_log(m,s)
#define free(x) debug_ext::debug_mem_allocation(__FILE__,
__LINE__, __FUNCTION__).free_and_nullify(x, #x)
#else
#define DELETE_ARRAY_AND_NULLIFY delete []
#endif
Jul 19 '05 #1
2 6958

"foo" <ma*******@axter.com> wrote in message news:c1**************************@posting.google.c om...
I'm creating a debug class called debug_mem_allocation for the purpose
of finding memory leaks.
I used macro's to replace the new and delete operators.
My problem is with trying to replace the delete[] operator with a
macro.


You can't replace delete[] with a macro. You can only replace identifires.
Furthermore your code, even with delete, is fraught with perils.
Nothing says that delete has to be given a modifable lvalue.

Jul 19 '05 #2
foo
"Ron Natalie" <ro*@sensor.com> wrote in message news:<TK********************@giganews.com>...
"foo" <ma*******@axter.com> wrote in message news:c1**************************@posting.google.c om...
I'm creating a debug class called debug_mem_allocation for the purpose
of finding memory leaks.
I used macro's to replace the new and delete operators.
My problem is with trying to replace the delete[] operator with a
macro.


You can't replace delete[] with a macro. You can only replace identifires.
Furthermore your code, even with delete, is fraught with perils.
Nothing says that delete has to be given a modifable lvalue.

That's why there's an overloaded version of operator+()
template<typename T>
void operator+(const T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
}
The overloaded version with the const does not try to set the pointer
to a NULL, but it does do the logging.
If delete is given an rvalue, this operator is used instead of the
following:
template<typename T>
void operator+(T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
ptr = NULL;
}

The only problems I've seen with this is with VC++ in which I still
get compile errors for something like the following:
delete this;
Which gives me the following error:
error C2667: '-' : none of 2 overload have a best conversion
error C2593: 'operator -' is ambiguous

However, I don't get this error with the GNU compiler, so I'm thinking
this is a bug in VC++.
In any case it's rare to see this type of code, and it can easily be
modified to the following:
foofoo* t = this;
delete t;
Which makes VC++ happy.
fraught with perils suggest to me many problems. Is there something
else you see wrong with the code?

And I still think there's a C++ Guru out there who can think of a
better work-around for delete[] operator then what I have.
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
2
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data...
1
by: Michael Eisner | last post by:
I have an MS Access 8.0 (Office97) program that has a form called FO-008 that I need to replace on several users computers in different locations without me being there doing it manually. I'm...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
0
by: Rave | last post by:
This is a long shot, but I thought I'd try it. I am currently using excel as an inventory tool. I currently have a hand-held scanner plugged into a laptop for reading barcodes. Using the "Find and...
2
by: Scott | last post by:
I have a macro set up to delete a table and then it imports an up-to-date copy of the table. Every once in a while the table gets deleted but the new one isn't imported, I assume the user is...
1
newnewbie
by: newnewbie | last post by:
Every week I get an Excel file that needs to be modified to be imported in Access. Modification includes columns renaming, deleting some of them, changing their order and data type, etc. I...
6
by: simon.robin.jackson | last post by:
Ok. I need to develop a macro/vba code to do the following. There are at least 300 corrections and its expected for this to happen a lot more in the future. Therefore id like a nice...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.