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

Posted on 1997-04-29
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 =)
Question by:mc2
  • 4
  • 4

Accepted Solution

LucHoltkamp earned 100 total points
ID: 1163065
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.

Author Comment

ID: 1163066
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


Expert Comment

ID: 1163067
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)

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Author Comment

ID: 1163068
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.


Expert Comment

ID: 1163069
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.

Author Comment

ID: 1163070
> 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?


Expert Comment

ID: 1163071
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:

Author Comment

ID: 1163072
I've found the answer.

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now