Jump to content

  • Log In with Google      Sign In   
  • Create Account


how does a (the) heap work?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Zoomby   Members   -  Reputation: 122

Like
Likes
Like

Posted 04 January 2003 - 11:59 PM

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

Sponsor:

#2 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 05 January 2003 - 02:18 AM

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.

#3 SiCrane   Moderators   -  Reputation: 9158

Like
Likes
Like

Posted 05 January 2003 - 09:02 PM

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.

#4 Zoomby   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 January 2003 - 03:22 AM

thanks,

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

bye
chris

#5 null_pointer   Members   -  Reputation: 289

Like
Likes
Like

Posted 06 January 2003 - 06:48 AM

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.

#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 06 January 2003 - 08:06 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS