Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a Memory Pool in which I am using a STL map to store information about the allocated memory. I have globally overloaded new/delete.The problem is that map is calling the overloaded new which is creating some problem.



C++
int *iptr = new int[10] ----> should call overloaded new[] 
delete [] iptr -------------> should call overloaded delete[] 
map.insert(10) ------------->; should call default new[] ( which are in new.h) 
map.erase()  ---------------> should call default delete[] //(which are in new.h)


Is it possible to achieve?
Posted

You have a few options...

0. Don't overload new/delete globally (you can always define your own allocator).
1. Separate calls into files that require different allocators (like what John suggested).
2. Use a placement new instead of overriding it all together (should be all you need for your memory pool).
 
Share this answer
 
Comments
barneyman 1-Jun-12 22:27pm    
As Albert suggests in #0, all the std containers allow you to create your own allocater class ... use that
The problem here is as soon as you override the global news and deletes you're making your new versions the default. They're the court of last resort, if they fail the runtime library won't step in with another version: The new/delete you define will prevent the versions that come with your runtime library from even being linked.

Anyway, Here're three suggestions:

- Create a custom allocator and let the map use it. You could use malloc and free (ugh, spit, spit, ugh) or operating system features (double ugh, barf, barf, heave) to implement it. Allocators are a bit of a fiddly interface but it can be done.

- Turn the problem on it's head - don't override global new and delete. Instead override them on a class by class basis, which will give you a lot more control and perhaps efficiency as you can implement heaps or pools for different sized classes.

- Use a static data structure that doesn't resize itself. A sorted fix sized array of objects means you can access elements as fast using a binary search as a std::map. You'll have to profile your app to try and get a handle on what's the correct size for you.

My advice would be don't override any of the global news and deletes. You end up fiddling with pointers and "things man was not meant to know." Overriding anything global is a bit like having global state in a program - a pain in the bum to unit test. If you need to customise stuff try using class specific new/delete or a custom allocator for for collections.
 
Share this answer
 
Comments
nv3 1-Jun-12 17:52pm    
Fully agree!
You could give your new new a new name, such as my_new.
Then in the files where you want to use your own new use a macro...

#define new my_new

I think that would do it.
 
Share this answer
 
Technically you could create a library containing two functions default_new and default_delete, which internally call the default functions. Since you'd build the library without overloaded new/delete, it should work all right.

However, I suspect the performance loss you incur from calling upon a library may eat up much if not all of the gain you hoped to win from using a memory pool in the first place...

The better solution would be to not globally overload new/delete. Memory pools are most useful for lists and maps, so if you just provide special allocators for these that should cover most of your performance problems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900