• Advertisement

Archived

This topic is now archived and is closed to further replies.

how does a (the) heap work?

This topic is 5494 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi! can someone explain how a (the c++) heap works? Or do you know a good site where it is explained? How does the heap itself allocate memory? Is it possilbe to write own malloc-functions without touching the standard malloc-functions? bye chris

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
The C runtime provided by for instance Visual C++ implements malloc for you. Malloc will handle allocation of memory from the operating system.

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.

Share this post


Link to post
Share on other sites
On *NIX platforms the memory managed by malloc is generally allocated from the operating system by the sbrk() system call.

http://gee.cs.oswego.edu/dl/html/malloc.html
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.

Share this post


Link to post
Share on other sites
thanks,

but I use Windows. Is the "RtlAllocateHeap" function documented in MSDN? I could''nt find it.

bye
chris

Share this post


Link to post
Share on other sites
quote:
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.

quote:
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.

quote:
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.

quote:
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Zoomby
Is the "RtlAllocateHeap" function documented in MSDN? I could''nt find it.
RtlAllocateHeap is a kernel32.dll internal function and should never be called directly. Instead you should use HeapAlloc or LocalAlloc.

You can read more about NT''s memory handling here: http://www.winntmag.com/Articles/Index.cfm?IssueID=56&ArticleID=3686

Share this post


Link to post
Share on other sites

  • Advertisement