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)

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

821 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