Overloading delete problem!!
I've written a nice memory manager that handles allocating/freeing memory, memory leaks, uninitialized memory, memory statistics, etc, but I have one major problem.
I can overload new adding extra parameters but I can't do the same for delete. Here's my code:
void *operator new(size_t size, const char *szFilename, const int iLine);
void operator delete(void *ptr, const char *szFilename, const int iLine);
#define DEBUG_NEW new(__FILE__, __LINE__)
#define DEBUG_DELETE delete(__FILE__, __LINE__)
#define new DEBUG_NEW
#define delete DEBUG_DELETE
void main()
{
int *x = new int;
delete x;
}
The new calls my overloaded function and works like a charm, but the delete gets an error message on compile: "delete : cannot delete objects that are not pointers" and "error C2146: syntax error : missing ';' before identifier 'x'".
Does anyone have ANY clue why I can overload the new but not the delete???
Edited by - Houdini on 8/25/00 10:36:13 PM
Edited by - Houdini on 8/25/00 10:37:25 PM
Edited by - Houdini on 8/25/00 10:39:11 PM
- Houdini
You cannot had parameters because the new parameters would not mean something. The reason being that when you call delete, the destructor is called and then the real delete is called or you overloaded one so, the only thing that is passed to the delete is a meaningless chunk of memory.
Actually, I CAN pass extra parameters, but in a different way. After the precompiler replaces the macros, the code looks like this:
x = new(__FILE__, __LINE__) int;
delete(__FILE__, __LINE__) x;
As it turns out, the ''int'' in the first line has to be OUTSIDE the ()''s, but the pointer (the variable x) in the second line has to be INSIDE, like so:
delete(__FILE__, __LINE__, x);
But, there is no way (that I know of) to create a macro that would replace delete AND accept the variable you are trying to delete, unless you include the variable inside parens, ie: delete(x);
Anyone have any ideas of how to create a macro that would replace the delete operator AND get the variable?
x = new(__FILE__, __LINE__) int;
delete(__FILE__, __LINE__) x;
As it turns out, the ''int'' in the first line has to be OUTSIDE the ()''s, but the pointer (the variable x) in the second line has to be INSIDE, like so:
delete(__FILE__, __LINE__, x);
But, there is no way (that I know of) to create a macro that would replace delete AND accept the variable you are trying to delete, unless you include the variable inside parens, ie: delete(x);
Anyone have any ideas of how to create a macro that would replace the delete operator AND get the variable?
- Houdini
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement