Advertisement

Overloading New and Delete

Started by February 06, 2000 10:10 PM
8 comments, last by KaneTHP 24 years, 7 months ago
Just wondering what anyone can tell me about overloading the new and delete operators in C++. No question in particular...just want to know what people''s experiences are with this kind of thing Heres an example of the kind of code I''m using void *::operator new(size_t size) { if(!CustomNewFunctionPtr) { return malloc(size); } else return CustomNewFunctionPtr(size); } The custom new function lets me use my own memory manager that preallocates large blocks of memory (to help reduce memory fragmentation - at least thats the idea), track the number of allocs / deallocs and make sure that they match in the end, etc. I was just wondering how other people have designed such memory managers and what kind of pitfalls, if any, they encountered. Also will I run into any problems using malloc and free (I rarely use these ) and is the source to these functions available anywhere - I want to see how they work. As this is unexplored territory for me, any (and all) information about overloading new / delete, good memory manager design, and the like would be appreciated. - Thanx JKaneBaggett
From what I see, you are not exactly overloading, just overriding. I could be wrong, so correct me if I am, but overloading means something like below

void* operator new(size_t i_Size, char* i_strFileName, int i_nLine);

meaning extra parameters.


Anyway, a pitfall I encountered before was to never never use iostream or fstream(stream related classes) within these operators(new and delete). This is because these stream classes will call new and delete within some function calls, and it will cause the program to crashed. Nasty experience.

On a side note, no one i met knew how to correctly call the overloaded delete operator.
Advertisement
well yes..I just simply wanted to force new and delete to go through my own memory manager so that I could control how it memory is allocated and track the amount of allocs/deallocs (I always seem to miss a delete in there somewhere, or reuse a pointer thats been deleted) - pretty much just to add some error checking.. The only really problem I forsee is that I want to be able reroute malloc/free to my own memory handlers as well. But the memory handler itself uses malloc and free - it falls into a nasty recurisive kind of mess. So can anyone explain exactly how these work, or does anyone have the source to them? Or maybe another solution that I''m missing?

I rarely use iostream (Though it is pretty cool) but its good to know that.

As for the rerouted delete - what I''ve been using is similar to the new...

void (*CustomDeleteFunctionPtr)(void *ptr) = 0;

//...

void ::operator delete(void *ptr)
{
if(!CustomDeleteFunctionPtr)
{
free(ptr);
}
else
CustomDeleteFunctionPtr(ptr);
}

However, the version with the brackets (delete[]) for arrays has me at a bit of a loss..


As I said - this(memory management) isn''t my area of expertise - I''m kinda shooting in the dark here - but I''m trying to learn...
Two projects that have source using overridden new and deletes (and delete[]s for that matter):
-The ClanLib Source has overrides for new and delete for leak detection. (At least in the 2.3 source, I haven''t looked at the 3.x source yet).
-Boehm''s garbage collection library also has a header for C++ overriding new and delete.

ClanLib is sort of the basics of the idea, Boehm''s is more like putting the idea on crack.

Other nifty use for overriding new and delete: object instance counting.
As far as I know you cannot override malloc() and free(), because they are not operators as new and delete. You might be able to use #define to change them, but then you must include this statement before everything else.
For delete[] you just need to figure out how to ''cheat'' and keep track of the number.

One way compilers do this is to allocate the memory needed plus an extra int value. The first DWORD holds the size of the array or elements and the pointer returned via new[] is actually the second DWORD in the allocated space. then when you call delete[] it knows to start one step back from the _this_ pointer and how to calculate how much memory you use.

And, of course, make sure you really need to do your own memory allocation. If you do your own allocation TEST, TEST, TEST. Even then you''ll probably end up with some bugs that''ll drive you crazy.

-the logistical one-
http://members.bellatlantic.net/~olsongt
-the logistical one-http://members.bellatlantic.net/~olsongt
Advertisement
I''ve written a memory manager myself for a couple projects of mine. I didn''t overload/override new and delete (I didn''t know it was possible), so it uses a standard functional interface (MEM_Alloc, MEM_Free, etc.).

I can send you the source to this if you want.

I guess this is where most people put a famous quote...
"Everything is funnier with monkey''s" - Unknown
--------------------------I guess this is where most people put a famous quote..."Everything is funnier with monkey''s" - Unknown
Ah..so thats how they do delete[] (guess that follows the KISS principal

The problems I''m hitting now are...

1) When I''m calling delete I''m also setting the pointer to zero so that it can''t access the memory any more - how can I (or can I?) see if there are any other pointers pointing to the same memory block

2) How can I keep the pointer from pointing outside the memory block after various pointer arithmetic is applied? or a function writing over their block of memory and into the next?

Is there a simple way to to this that I''m missing or is it as complicated (or near-impossible) as I think it is?

It was questions like these that led to the development of Java. =)

In C/C++ it''s to much work to prevent a programmer from setting a pointer to memory that isn''t allocated to be worth it.

You might be able to overload the ''='' operator though.
The operators new and delete are just like malloc() and free(), except they were put in there for this reason: malloc() and free() don't (and can't) call the constructor and destructor of a class. new and delete just allocate memory like malloc() and free(), and then call the constructors and destructors (which may then call new and delete to allocate other objects...etc.).

new[] and delete[] allocate arrays, call any necessary constructors/destructors, and keep track of the array size themselves.

However, the best way by far (and actually the easiest) to learn more about memory management, is to buy a good book on C++ (not MFC, not Windows, just C++). I know that "C++ for Dummies: 3rd Edition" covers them very well. I had absolutely no questions concerning pointers when I was done with that book. (in fact, that's where I got the info for this reply) There's lots of other good books though.

Spellbound: Overloading the = operator won't work, because it operates on the object, not your pointer to that object.

Good Luck!


- null_pointer


Edited by - null_pointer on 2/8/00 8:30:33 AM

This topic is closed to new replies.

Advertisement