how does a (the) heap work?
Members - Reputation: 122
Posted 04 January 2003 - 11:59 PM
Posted 05 January 2003 - 02:18 AM
For instance malloc in the Visual C++ runtime library (msvcr70.dll or msvcr70d.dll) will eventually call RtlAllocateHeap in kernel32.dll, which will switch to kernel mode to allocate memory from the memory manager in NT.
The debug and release versions of the Visual c++ runtime are a bit different (more debug help and less performance in the debug version). If you installed the CRT source during visual studio install (you should!) you can look in C:\Program Files\Microsoft Visual Studio .NET\Vc7\crt\src to find the malloc implementation (malloc.c, dbgheap.c).
Since it''s relatively costly to allocate memory from the operating system, a memory manager often keeps some allocated memory ''cached'', so that when you call malloc(size), malloc will first look in a (quite small) list of memory blocks to see if one is available for your call, and if so give you that block and remove it from the list. Otherwise a call to the OS is necessary. When you call free, either memory is put in that list of free blocks, or returned to the OS. There are tons of optimizations that could be done, but my description should at least give you a clue how it works.
Moderators - Reputation: 11140
Posted 05 January 2003 - 09:02 PM
describes a common, specific, *NIX malloc algorithm.
In general I wouldn''t recommend using sbrk() to implement your own malloc-like functions as that can mess with the standard library dynamic memory allocations. I believe on most *NIX platforms you could use mmap(), but it tends to be slow. If you want to implement custom memory management, it usually turns out easier (and more portable) if you implement it on top of existing malloc functionality.
Members - Reputation: 289
Posted 06 January 2003 - 06:48 AM
can someone explain how a (the c++) heap works?
Most C++ programmers never touch the heap. In most cases you would be better served by the free store.
Or do you know a good site where it is explained?
There should be a lot of good sites. Try google. Or get a book such as The C++ Programming Language by Bjarn Stroustrop.
How does the heap itself allocate memory?
By making a call to the operating system''s memory manager, which in turn works with the hardware to manage RAM in chunks called pages. More information on this can be found in your processor manufacturer''s architecture manuals and in semi-architecture-agnostic books like Modern Operating Systems by Andrew Tanenbaum.
Is it possilbe to write own malloc-functions without touching the standard malloc-functions?
Yes, if you make the system calls yourself. It is probably one of those things that if you do not understand how to do it, you have no idea when it ought to be used.
Posted 06 January 2003 - 08:06 AM
quote:RtlAllocateHeap is a kernel32.dll internal function and should never be called directly. Instead you should use HeapAlloc or LocalAlloc.
Original post by Zoomby
Is the "RtlAllocateHeap" function documented in MSDN? I could''nt find it.
You can read more about NT''s memory handling here: http://www.winntmag.com/Articles/Index.cfm?IssueID=56&ArticleID=3686