473,729 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to replace delete[] with a macro

foo
I'm creating a debug class called debug_mem_alloc ation 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_AN D_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_alloc ation
{
public:
enum ACTION_SWITCH{A llocateMem, DeAllocMem, GenerateReport} ;
debug_mem_alloc ation(const char* FileName, int LineNo, const char*
FunctionName)
:m_FileName(Fil eName), m_LineNo(LineNo ), m_FuncName(Func tionName){}
template<typena me T>
void operator+(T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
ptr = NULL;
}
template<typena me T>
void operator+(const T &ptr)
{
Log(ptr, DeAllocMem);
delete ptr;
}
template<typena me T>
void operator-(T &ptr)
{
Log(ptr, DeAllocMem);
delete [] ptr;
ptr = NULL;
}
template<typena me T>
T& operator<<(T &ptr)
{
Log(ptr, AllocateMem);
return ptr;
}
template<typena me T>
void free_and_nullif y(T &memblock , const char* VariableName)
{
Log(memblock, DeAllocMem);
free(memblock);
memblock = NULL;
}
void *malloc_and_log (size_t size );
void *realloc_and_lo g(void *memblock, size_t size );
static void SendOutCurrentL og();
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_DEB UG_MEM_ALLOCATI ON)
#define new debug_ext::debu g_mem_allocatio n(__FILE__, __LINE__,
__FUNCTION__)<< new
#define delete debug_ext::debu g_mem_allocatio n(__FILE__,
__LINE__, __FUNCTION__)+
#define DELETE_ARRAY_AN D_NULLIFY debug_ext::debu g_mem_allocatio n(__FILE__,
__LINE__, __FUNCTION__)-
#define malloc(x) debug_ext::debu g_mem_allocatio n(__FILE__,
__LINE__, __FUNCTION__).m alloc_and_log(x )
#define realloc(m,s) debug_ext::debu g_mem_allocatio n(__FILE__,
__LINE__, __FUNCTION__).r ealloc_and_log( m,s)
#define free(x) debug_ext::debu g_mem_allocatio n(__FILE__,
__LINE__, __FUNCTION__).f ree_and_nullify (x, #x)
#else
#define DELETE_ARRAY_AN D_NULLIFY delete []
#endif
Jul 19 '05 #1
2 6992

"foo" <ma*******@axte r.com> wrote in message news:c1******** *************** ***@posting.goo gle.com...
I'm creating a debug class called debug_mem_alloc ation 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******* *************@g iganews.com>...
"foo" <ma*******@axte r.com> wrote in message news:c1******** *************** ***@posting.goo gle.com...
I'm creating a debug class called debug_mem_alloc ation 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<typena me 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<typena me 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
912
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 nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
2
14929
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 in the format that I need it in order to create the reports that we use. So far this has proven to be successful for the reports that I am doing and the data that I am pulling into it. I just have one challenge that may require a lot of work and I...
1
2014
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 trying to perform the replacement by using an external MS Access program that contains the replacement form also called FO-008. I need to keep the same form name since the form contains relationships to other forms and quarries in the program. ...
3
4650
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 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
3
5407
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 example first:
0
2797
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 Replace" fuction, I scan the merchandise which then searches the spreadsheet for the matching inventory number. When it is found, I highlight that cell in yellow. After scanning and coloring all of my inventory I can then see exactly what is...
2
13421
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 closing the database before the macro finishes running. The next time the macro runs it comes up with an error becuase the delete table part of the macro can't find the table. Is there any way to have the macro check to see if the table is there and...
1
6954
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 recorded a macro to do all this for me last week, and this week was very happy to discover the extract I am getting does not guarantee the order of columns, and my macro was based on the order (select column A, cut it, paste it in column Z, etc)...not the...
6
2233
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 button that does this all for me. In my head the method should go something like this:
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9280
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.