views:

3899

answers:

6

There are lots of method to allocate memory in windows enviorment, such as VirtualAlloc/HeapAlloc/malloc/new.

Thus , what's the difference among them?

+3  A: 

In outline:

  • VirtualAlloc, HeapAlloc etc. are Windows APIs that allocate memory of various types from the OS directly. VirtualAlloc manages pages in the Windows virtual memory system, while HeapAlloc allocates from a specific OS heap. Frankly, you are unlikely to ever need to use eiither of them.

  • malloc is a Standard C (and C++) library function that allocates memory to your process. Implementations of malloc will typically use one of the OS APIs to create a pool of memory when your app starts and then allocate from it as you make malloc requests

  • new is a Standard C++ operator which allocates memory and then calls constructors appropriately on that memory. It may be implemented in terms of malloc or in terms of the OS APIs, in which case it too will typically create a memory pool on application startup.

anon
+3  A: 

VirtualAlloc is a specialized allocation from the OS VM system. Allocation's must be made at on an allocation granularity which is architechture dependent. It is one of the most basic forms of memory allocation. VM allocations can take several forms, memory is not nessisarially dedicated or physically backed in RAM (though it can be). It is typically a special purpose type of allocation, either vary large, needs to be shared, must be aligned on a perticular value (performance reasons) or the caller need not use all of this memory at once... etc...

HeapAlloc is essentially what malloc and new both eventually call. It is designed to be very fast and useable under many different types of scenerio's a general purpose allocation. It is the "Heap" in a classic sence. Heap's are actually setup by a VirtualAlloc, which is what is used to initially reserve space from the OS, after this the space is initialized, which is when various tables, lists's and other data structures are configured to maintain and control the operation of the HEAP. Some of that is in the form of dynamically sizing (growing and shrinking), adapting to perticular usages (frequent allocations of some size), etc..

new and malloc are somewhat the same, malloc is essentially an exact call into HeapAlloc( heap-id-default ); new however, can configure object's for C++. C++ will store vtable's onto the heap for each caller. These vtable's are redirect's for execution and form part of what gives C++ it's oo charicteristics like inheratence, function overloading, etc...

Some other common allocation methods; _alloca() and _malloca are stack based, FileMappings's are really VirtualAlloc'd and set with perticualr bit flags's which designate them FILE.

Most of the time, you should allocate memory in a way which is consistant with the use of that memory ;). new in C++, malloc for C, VirtualAlloc for massive or IPC cases.

*** Note, large memory allocation's by HeapAlloc are actually shipped off to Virtual Alloc after some size (couple hundred k or 16 MB or something I forget, but fairly big :)

*** EDIT I brefly remarked about IPC and VirtualAlloc, there is also something very neat about a related VirtualAlloc which none of the responder's to this question have discussed.

VirtualAlloc Ex is what one process can use to allocate memory in a different process's address space. Most typically, used in combination to get remote execution in the context of another process via CreateRemoteThread (simular to CreateThread, the thread is just run in the other process).

RandomNickName42
+12  A: 
Doug
Doug: VirtualAlloc is not strictly limited to 4kb allocations, it's the size returned by GetSystemInfo(), SYSTEM_INFO::dwAllocationGranularity. It's actually _RARELY_ 4kb.On my host, it's 64k, I suspect the same for you. 4KB is the minimum page size entry for the various discriptor tables in the x86 ABI. 4KB is the smallest size which can be indapendently permissioned, R/W/X, however it is not of any significance to VirtualAlloc. If you refer to the VirtualAlloc documentation, there is also the LARGE_PAGES option ( see http://msdn.microsoft.com/en-us/library/aa366568(VS.85).aspx ).
RandomNickName42
A: 

I am going to make it very simple for you:

VirtualAlloc => Allocates straight into virtual memory, you reserve/commit in blocks. This is great for large allocations, for example large arrays.

HeapAlloc / new => allocates the memory on the default heap (or any other heap that you may create). This allocates per object and is great for smaller objects. The default heap is serializable therefore it has guarantee thread allocation (this can cause some issues on high performance scenarios and that's why you can create your own heaps).

malloc => uses the C runtime heap, similar to HeapAlloc but it is common for compatibility scenarios.

In a nutshell, the heap is just a chunk of virtual memory that is governed by a heap manager (rather than raw virtual memory)

the last model on the memory world is memory mapped files, this scenario is great for large chunk of data (like large files). This is used internally when you open an EXE (it does not load the EXE in memory, just creates a memory mapped file).

Hope this helps

No hay Problema
+1  A: 

VirtualAlloc ===> sbrk() under UNIX HeapAlloc ====> malloc() under UNIX

Mrad Chems Eddine
A: 

And for an ocx for windows Mobile, what type of allocation i need to use?

This should be aseparate question.
Joachim Sauer