Sign in to follow this  
nuclear123

malloc vs api heapalloc

Recommended Posts

Zipster    2359
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
marsong    154
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
Jan Wassenberg    999
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this