What is the difference between malloc() and HeapAlloc()? As far as I understand malloc allocates memory from the heap, just as HeapAlloc, right?
So what is the difference?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You are right that they both allocate memory from a heap. But there are differences:
malloc()is portable, part of the standard.HeapAlloc()is not portable, it’s a Windows API function.It’s quite possible that, on Windows,
mallocwould be implemented on top ofHeapAlloc. I would expectmallocto be faster thanHeapAlloc.HeapAllochas more flexibility thanmalloc. In particular it allows you to specify which heap you wish to allocate from. This caters for multiple heaps per process.For almost all coding scenarios you would use
mallocrather thanHeapAlloc. Although since you tagged your question C++, I would expect you to be usingnew!