問題描述
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++,我希望你使用 new
!
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() 與 HeapAlloc()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!