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)


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.

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.


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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

758 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