Question

I'd like to know which method is recommended on Windows C programming: using malloc or the Win32 HeapAlloc (maybe VirtualAlloc?) function.

I've read the MSDN Memory Management Functions article and the MSDN articles regarding malloc and HeapAlloc, but they do not say which one should be used and in what situations.

Was it helpful?

Solution

Stick with malloc unless you have a compelling reason to use something different. It will be implemented underneath in terms of the OS memory allocation primitives, but there is no real advantage in diving down to that layer yourself.

A few API calls I believe need a memory block allocated from a Windows heap, but you will know when you come across them.

Or if you want to do something more advanced, like use shared memory, or need to control the permissions on the memory pages directly then you will need to look at the Windows API calls like VirtualAlloc.

OTHER TIPS

VirtualAlloc and friends can give you a bit of an edge if you do have heaps of data to process or if you need to go to the trouble of creating your own memory manager anyway.

Otherwise it's easier and of course more portable to just use malloc().

VirtualAlloc has this nifty feature called MEM_RESET, which invalidates the data in a block of memory, but keeps it allocated. This means if it's paged to disk, Windows won't bother to page it back in next time you access it. It's nice if you have many megs of data that can suddenly become unnecessary, but you'll soon have something else to fill the buffer up.

It also differentiates between reserving address space and actually requesting memory. There's some nice stuff there if you have a good reason to go to all that trouble.

One more thing: malloc() is guaranteed to be portable (at least to any ANSI-C implementation) and more elegant.

In some situations using functions such as HeapAlloc, HeapFree will make your life easier. One example would be: a big application where you need to allocate memory in one module (say in library1.dll) and free that memory in the main module (say program.exe). This can be done safely if you are using HeapAlloc, HeapResize and HeapFree functions, but cannot be done using C runtime library (eg malloc, free, resize).

BUT: If you don't have a good reason, you should stick with malloc/free/resize functions. Also, if you need to change permisions of the allocated memory (eg: to make if executable, etc), you should use functions such as VirtualAlloc, VirtualFree.

You could make a wrapper and leave the option to change the implementation details. You could even compare both options with your code and then decide.

Unlike Rob, I go the other way... Since I chose to code against the WinAPI, I use the native functions instead of C run-time ones, which are just a thin wrapper around them anyway.

With HeapAlloc you can have separate heaps for different tasks/subsystems. This may simplify dump analysis of large applications.

With malloc you can only use one heap, but you get some allocation optimizations that CRT authors may have implemented on top of OS HeapAlloc.

Going down to VirtualAlloc doesn't buy you much, unless you want to implement custom heap manager (your own set of Heap* functions).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top