Post by Jake MontgomeryWhat function (GlobalAlloc, HeapAlloc,VirtualAlloc or malloc) should be
used to allocate large blocks of memory (1-100MB). There are no issues
of passing the pointes across processes or between dlls.
GlobalAlloc - Use only when required, such as when dealing with certain
clipboard formats.
HeapAlloc - IIRC, it's documented to be a poor choice on Win9X for blocks >
4 MB. I've not found much reason to use this API.
VirtualAlloc - This one allows you to decommit memory you've freed, which
shrinks your process's address space and pagefile usage, which can be
desirable when talking about very large blocks. It also provides a
guaranteed and efficient way to start a buffer small and grow it
incrementally in place; as long as you don't need to exceed the maximum size
you designated when you reserved the memory, you can commit pages within the
reserved area without causing any reallocation, thus avoiding expensive
copying. It's also the basis for dynamically committing memory upon first
use when using SEH. On the downside, the granularity of allocations is
rather high, being the page size of the machine.
malloc - This is what a C program should generally use. It's portable and is
sufficient for most needs.
Post by Jake MontgomeryWhat are the speed implications of these different routines?
For allocating a single large block of memory, they're probably all about
the same, modulo things like the Win9X caveat concerning HeapAlloc.
Post by Jake MontgomeryFor malloc, does it matter which version of the CRT I link with?
The multithreaded CRT guards the heap with a single critical section, which
can be a bottleneck for some applications whose threads make frequent
allocations and end up contending for the heap a lot of the time.
Post by Jake MontgomeryI assume that once I have the memory, the speed of access to it will not
depend on how it is allocated?
Correct.
See this MSDN topic for a brief comparison of allocation methods and links
to further documentation:
Comparing Memory Allocation Methods
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/comparing_memory_allocation_methods.asp?frame=true
--
Doug Harrison
Microsoft MVP - Visual C++