malloc()vs. HeapAlloc() [英] malloc() vs. HeapAlloc()
问题描述
malloc()和HeapAlloc()之间有什么区别?据我所知,malloc从堆中分配内存,就像HeapAlloc一样,对吧?
What is the difference between malloc() and HeapAlloc()? As far as I understand malloc allocates memory from the heap, just as HeapAlloc, right?
那么有什么区别?
谢谢!
推荐答案
你是对的,他们都从堆分配内存。但有区别:
You are right that they both allocate memory from a heap. But there are differences:
-
malloc()
。 -
HeapAlloc()
不可移植,它是Windows API函式。
malloc()
is portable, part of the standard.HeapAlloc()
is not portable, it's a Windows API function.
很可能在Windows上, malloc
将在 HeapAlloc
之上实现。我希望 malloc
要比 HeapAlloc
更快。
It's quite possible that, on Windows, malloc
would be implemented on top of HeapAlloc
. I would expect malloc
to be faster than HeapAlloc
.
HeapAlloc
比 malloc
更具灵活性。特别是它允许你指定你希望分配的堆。
HeapAlloc
has more flexibility than malloc
. In particular it allows you to specify which heap you wish to allocate from. This caters for multiple heaps per process.
对于几乎所有的编码情况,你都会使用 malloc
,而不是 HeapAlloc
。虽然你标记了你的问题C ++,我希望你使用新
!
For almost all coding scenarios you would use malloc
rather than HeapAlloc
. Although since you tagged your question C++, I would expect you to be using new
!
这篇关于malloc()vs. HeapAlloc()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!