• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

## Recommended Posts

Hey, I''m trying to create a small memory manager because I''m allocating / deallocating classes a great deal, and it''s killing my windows resouces. Anyway, I overloaded new to take from a predefined memory pool, and I overloaded delete to free memory from a predefined memory pool. Here''s the problem. I''m creating all of these as global overloads of new and delete. This causes stuff to use my overloaded delete that I don''t want use my overloaded delete (including things like the IO stream classes). My new works fine, cause it''s an overload of placement new: void *new(size_t size, const char *file, int line); I have a delete the same way: void delete(void *ptr, const char *file, int line); However, the MSVC++ compiler is telling me any time I use the overloaded placement delete (e.g. delete(__FILE__, __LINE__) ptr that I can''t delete something that isn''t a pointer. What''s going on and how do I fix it? Is thi a problem in microsoft''s compiler or am I missing something?

##### Share on other sites
AFAIK, there is no "placement delete".

The original use for placement new is to build an object in allocated memory. To destroy an object without liberating the memory, you directly call the destructor, not delete.

The canonical way to have your operator new/delete used only in some places is to make them static member functions of a mixin policy class. Classes that need to use your memory allocation system need to inherit from the policy class.

However, if you are planning to create a library, this may have the undesirable side-effect that users of your library won''t be able to choose not to use your memory allocator if you decided to use it in one of your class (by deriving it from the policy class).

The next step is then to make the memory allocation behavior a template parameter of your class. Which is exactly what STL allocators are meant to do, even though they are rather awkward to use.

Of course none of this may help you with your problem.

##### Share on other sites
Okay, "placement delete" is probably a bad name for it, considering no "placement" is actually occuring. "Delete w/ paramaters" is probably better. Here we go with the fun

If I have an operator new like this:
void *operator new(size_t size, const char *file, int line);

I have to have an operator delete like this:
void operator delete(void *ptr, const char *file, int line);

Or I get an error about new not having a matching delete and can't free memory if an exception is thrown blah blah blah. However, I can't call that special form of delete. It won't let me. Any time I try to call it, I get something about "can not delete an object that is not a pointer." From things I've read, there should be a way do execute such an operator. What am I missing?

Also, anyone know why delete isn't in MSDN?

Edited by - Warden on January 13, 2002 12:47:06 PM

##### Share on other sites
Here''s what I meant : as far as I can tell you cannot add extra parameters to delete. The prototype is always

void operator delete( void* ptr, size_t size );

And the only way to call it (array issues aside) is delete ptr

##### Share on other sites
Delete IS in the MSDN...

Look under "delete operator" (just checked on my local version)

##### Share on other sites
And I agree with Fruny...

If you overload an operator, you need to make it exactly the same as the "previous" (original) version was.

Since you look to be using the __FILE__ and __LINE__ macros, that makes me think that it''s trying to delete __FILE__, and that won''t work because it''s not allocated memory.

The reason the operators have to be *exactly* the same (from what I can tell) is that the original operators will take precedence over anything with the same name UNLESS it''s overridden.

One suggestion is to call it MyDelete and MyNew (or something less stupid sounding...nevermind, something MUCH less stupid sounding) and use that to manage your memory and stuff. You could define that on a global scope without any problems of interfering with any other memory stuff going on.

That''s my take, anyway. I''ve never TRIED to overload those operators before, as I never had a reason to yet. Eventually I plan to get around to it (just like EVERYTHING else....).

##### Share on other sites
quote:
Original post by Dragonskin
The reason the operators have to be *exactly* the same (from what I can tell) is that the original operators will take precedence over anything with the same name UNLESS it''s overridden.

Well, the thing is that operator new supports extra arguments via the "placement new syntax".

If you write: ptr1 = new( ptr2 ) int;
the compiler will call operator new( sizeof(int), ptr2 ) and the constructors (well not for int )

And this syntax can be extended to a larger number of arguments, hence the trick with __FILE__ and __LINE__.

But you can''t do it with delete.

##### Share on other sites
Yeah, I found operator delete right after I posted that. Ugh.

Arg, that''s so wrong. I should be able to overload delete the same way I overload new considering it requires that a matching delete be written anyway. As it is, if I just have the one parameter as a global override, things I didn''t write try to use my delete operator (like ostream). That really sucks!

Anyway, to respond to why don''t I just make "MyNew / MyDelete" functions, is because I''ve already got huge amounts of code using new and delete straight. Switching it over would be a big huge pain in the ass.

##### Share on other sites
Fruny: Me sees.....

I just looked up both on MSDN and I see the difference.

Interesting that they did that, though...reminds me of the constructor/destructor differences. Weird.

Edited by - dragonskin on January 13, 2002 1:29:22 PM

• ### Forum Statistics

• Total Topics
628281
• Total Posts
2981801

• 10
• 11
• 17
• 14
• 9