Hello everyone,
Any practical experiences of VirtualAlloc v.s. malloc? From MSDN, I can not find any comparisons between the two methods.
thanks in advance,
George
Hello everyone,
Any practical experiences of VirtualAlloc v.s. malloc? From MSDN, I can not find any comparisons between the two methods.
thanks in advance,
George
VirtualAlloc is the better choice if you want to fully control how your allocation is done, particularly if you allocate HUGE amounts of memory. For general allocations, it's better to use malloc - the reason being that VirtualAlloc is done in the kernel, malloc is part of the C-runtime (it will call VirtualAlloc or some such to get big chunks of memory that it then split up into smaller pieces).
As you can see if you read the docs and understand about the memory mapping techology, the VirtualAlloc can not allocate small pieces of memory - the smallest "chunk" would be at least 4KB.
Actually, malloc() uses "HeapAlloc" rather than VirtualAlloc. But VirtualAlloc is still best for LARGE chunks of memory, or when you have special requirements that aren't fulfilled by malloc() - for example the memory needs to be protected in special ways, you want to get exceptions when there is a write to the memory or some such.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
VirtualAlloc does two things. First, it reserves address space. Second, it commits memory to that address space. Those are very low-level operations. What VirtualAlloc doesn't do is keep the extensive management information that is required to make small allocations. It only does pagewise allocations.
HeapAlloc keeps those data structures. HeapAlloc is pretty much the same as malloc, except that it is part of the Win32 API and not the CRT.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
To use HeapAlloc you need to first do HeapCreate, to create a chunk of memory to allocate from. HeapAlloc is then used to "split" the chunk into smaller portions.
As stated, VirtualAlloc is for reserving and/or committing a large chunk of memory - useful when you need megabytes. Committing here means that the memory map is filled in with ACTUAL memory pages [4KB portions], rather than just "this memory exists in the process space, but there's actually no real memory there" which is "reserving". If the memory isn't committed when it's being accessed, it will be comitted in the page-fault handler. This is handy if you want to reserve a really large chunk of memory, but you don't necessarily need to fill all parts of that memory with data [e.g. a HUGE hash-table, or a table indexed by some "random" numbers, telephone numbers or some such, where some ranges of numbers isn't actually used - so it's no point in actually taking up REAL memory for those positions].
If you are doing regular programming, use malloc(). What I'm trying to say is, malloc() is a good average function. It's only when something malloc does is unsuitable for your application that you may need something else: Really big allocations, "sparse" allocations [don't commit memory unless it's actually used], special protection (run-time generated code, which requires VirtualProtect to set the execute-disable bit to zero for example - malloc can't do that).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.