• Advertisement
Sign in to follow this  

malloc vs api heapalloc

This topic is 2824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

so what exactly is the difference between malloc and heapalloc api function? why would u want to use one over the other?

Share this post


Link to post
Share on other sites
Advertisement
HeapAlloc is a Windows API function, whereas malloc is a C library function. On Windows, malloc calls HeapAlloc internally, however malloc is standard C so it's completely portable and will be implemented appropriately on any target platform.

You'd really only use HeapAlloc directly (or any of the other flavors of allocation on Windows) if you had specific need for their additional functionality, i.e. creating and allocating from multiple heaps.

Share this post


Link to post
Share on other sites
I thought malloc was suppose to be an efficient allocation mechanism on top of HeapAlloc, but looking at the crt source code you can see that malloc (and operator new) pretty much just calls HeapAlloc directly, so from a logic standpoint they are effectively the same. (Maybe HeapAlloc already is an optimized memory allocator?)

Share this post


Link to post
Share on other sites
Quote:
malloc pretty much just calls HeapAlloc directly

That's true on x64, but there used to be a small-block heap (and an older still implementation) on top of HeapAlloc.

Quote:
why would u want to use one over the other?

HeapAlloc can be told not to take a lock, so that might be useful for avoiding serializing multiple threads. (While that's better than malloc, I'd prefer TCMallor or per-thread pools for such use cases.)

Share this post


Link to post
Share on other sites
Its usually better to use calloc which is a somewhat safer replacement for malloc.

Calloc/Malloc are both standard C library routines and portable....

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement