We help IT Professionals succeed at work.

Overloading new / malloc, free, etc. with user-defined heaps

mc2 asked
Medium Priority
Last Modified: 2006-11-17
Hi, I'm looking for info on how to overload new() (or malloc, for that
matter) so it takes memory from a user-defined block of memory instead
of the heap.

For example, if I have a 16MB chunk of memory which has already been
allocated, I would like to use new, malloc, delete, free, etc. so that
they can use this block of memory instead of the heap.  Also, I'd like
other functions that get memory (like string functions) to use this
memory instead.

Something like

ptr = new (my_type, &my_heap);

I basically need it to handle memory in a user-defined block, so garbage
collection is a good idea (re-aligning spaces to use contiguous memory
and get rid of holes).

Thanks =)
Watch Question

As far as I know you cannot replace the actual memorymanagement code used by new and delete, except for user-defined classes (just overload it, see your help for detailes).
So you have to write own functions (my_malloc, my_free) and use them in a c-style for intrinsic types. Perhaps you could use a template class with statics: my_malloc an my_free.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


I was pretty sure it would come to redefining the memory-
management functions, therefore I need help on how to do that.
This is still covered by the original question.  If I knew
offhand how to write a new version of malloc() (and free), I
would've already done that =)

Any suggestions there?  Someone else suggested taking malloc()
and replacing the sbrk calls; I would appreciate a simpler
solution if one can be found (if not, I'll see what I can do with

Hi mc2,

I think my first question must be, what exacly are you trying to do, why do you want to replace malloc and free?? Are you shure thats the best solution?.

I don't have the sourcecode of malloc, but replacing sbrk seem to me dangerous behavour. Better use the operating-systems memorymanagement to allocate a large chunck, and use your own functions with it: my_malloc and my_free.
For example BC++ has functions to allocate mem from DOS and Windows, bypassing malloc and free. Furthermore: I'm quite sure the startup-code doesn't use malloc at all, it directly call's the OS memorymanagament functions. If I were you I wouldn't try to catch every memory allocation in the library, I think it's impossible (well, you could write you own compiler...)
Why do you want to replace the memorymanagement?? malloc and free are doing quite a good job. Perhaps you should leave malloc and free as is, and only overload the big memory consumers (your own classes)

A quite extreme Idea would be (if you use DOS) to catch the int21 Functions 48h and 49h and rewrite the OS memoryhandler for your program. I wouldn't try it though.  

Finally: malloc itself is quite a simple function, it implements a heap as a linkedlist without garbage collection. You only need garbage collection if you have a lot of trashing (multi-tasking for example) otherwise its enough to join adjacent free blocks.

Maybe you should switch to WIN32 (you can write CONSOLE app's that use 'DOS-STYLE' code in a flat memory model) or you can use an extender. Those memorymanagers are VERY good. (For example: WIN32 uses the paging/segmenting mechanism of the CPU to implement a trashfree virtual memory. Hard to beat)



What I'm doing is allocating a large chunk of shared memory.
Once I have that chunk (say, 16 MB or 64 MB), which more than
one program will access, I need something to handle the memory.
This is because I will be storing structures of variable length,
so I can't just store them at (start) (start+32) (start+64), etc.

There's substantial overhead involved in shared memory
allocation, and there's a limit on how many shared memory
segments I can use, so I just want to grab one big segment and
use that rather than a bunch of smaller ones.

So, of course, I need a memory manager for it.  With all the
other things I have to do, I'd prefer not to have to write
a memory manager, too! =)

There are drop-in replacements for the malloc library, so I was
considering altering one of those, but it's fairly in-depth
so I was hoping to be able to tell new() to use my heap instead
of the normal heap.

Just for reference, this is C++ under Linux 2.0.

If I were in your position I would write it from scratch (not so bad, malloc is quite simple, even with garbage collection on a seperate thread), because I don't know how to replace malloc or free safely. You should reject my answer to let someone else try to answer this.


> If I were in your position I would write it from scratch (not
> so bad, malloc is quite simple, even with garbage collection on
> a seperate thread)

Well, if you know how to do this, could you give me a little
more info?

Well, I don't know anything about Linux.
Do you want a memory-manager that handles shared memory between processes?? I wouldn't know how to do that.
What I can give you is some code for a small memorymanager that can be used IN a program. The manager allocates the memory at startup and there are functions like malloc and free to go with it. If that is what you want, mail me, and I send it to you
Luc (email: lholtkam@plex.nl)


I've found the answer.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.