C++ malloc() 与 HeapAlloc()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8224347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
malloc() vs. HeapAlloc()
提问by TCS
What is the difference between malloc() and HeapAlloc()? As far as I understand malloc allocates memory from the heap, just as HeapAlloc, right?
malloc() 和 HeapAlloc() 有什么区别?据我了解 malloc 从堆中分配内存,就像 HeapAlloc 一样,对吧?
So what is the difference?
那么区别是什么呢?
Thanks!
谢谢!
采纳答案by David Heffernan
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.
malloc()
是便携式的,是标准的一部分。HeapAlloc()
不可移植,它是一个 Windows API 函数。
It's quite possible that, on Windows, malloc
would be implemented on top of HeapAlloc
. I would expect malloc
to be faster than HeapAlloc
.
在 Windows 上,很有可能malloc
在HeapAlloc
. 我希望malloc
比HeapAlloc
.
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.
HeapAlloc
比 具有更大的灵活性malloc
。特别是它允许您指定要从哪个堆分配。这迎合了每个进程的多个堆。
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
. 虽然因为你标记了你的问题 C++,我希望你使用new
!
回答by Luis Miguel Huapaya
Actually, malloc() (and other C runtime heap functions) are module dependant, which means that if you call malloc() in code from one module (i.e. a DLL), then you should call free() within code of the same module or you could suffer some pretty bad heap corruption (and this has been well documented). Using HeapAlloc() with GetProcessHeap() instead of malloc(), including overloading new and delete operators to make use of such, allow you to pass dynamically allocated objects between modules and not have to worry about memory corruption if memory is allocated in code of one module and freed in code of another module once the pointer to a block of memory has been passed across to an external module.
实际上,malloc()(和其他 C 运行时堆函数)是依赖于模块的,这意味着如果你在一个模块(即 DLL)的代码中调用 malloc(),那么你应该在同一模块的代码中调用 free()或者你可能会遭受一些非常严重的堆损坏(这已经有据可查)。使用 HeapAlloc() 和 GetProcessHeap() 而不是 malloc(),包括重载 new 和 delete 操作符以利用它,允许您在模块之间传递动态分配的对象,并且如果在以下代码中分配内存,则不必担心内存损坏一旦指向内存块的指针传递给外部模块,一个模块并在另一个模块的代码中释放。
回答by Daniel Morin
With Visual C++, the function malloc()
or the operator new
eventually calls HeapAlloc()
. If you debug the code, you will find the the function _heap_alloc_base()
(in the file malloc.c
) is calling return HeapAlloc(_crtheap, 0, size)
where _crtheap
is a global heap created with HeapCreate()
.
对于 Visual C++,函数malloc()
或运算符new
最终会调用HeapAlloc()
. 如果您调试代码,您会发现函数_heap_alloc_base()
(在文件中malloc.c
)正在调用return HeapAlloc(_crtheap, 0, size)
where _crtheap
is a global heap created with HeapCreate()
.
The function HeapAlloc()
does a good job to minimize the memory overhead, with a minimum of 8 bytes overhead per allocation. The largest I have seen is 15 bytes per allocation, for allocations ranging from 1 byte to 100,000 bytes. Larger blocks have larger overhead, however as a percent of the total allocated it remains less than 2.5% of the payload.
该函数HeapAlloc()
在最小化内存开销方面做得很好,每次分配至少有 8 个字节的开销。我见过的最大的是每个分配 15 个字节,分配范围从 1 个字节到 100,000 个字节。较大的块具有较大的开销,但是作为分配总量的百分比,它仍然少于有效载荷的 2.5%。
I cannot comment on performance because I have not benchmarked the HeapAlloc()
with a custom made routine, however as far as the memory overhead of using HeapAlloc()
, the overhead is amazingly low.
我无法对性能发表评论,因为我没有HeapAlloc()
使用定制的例程对其进行基准测试,但是就 using 的内存开销而言HeapAlloc()
,开销低得惊人。
回答by Cheers and hth. - Alf
malloc
is a function in the C standard library (and also in the C++ standard library).
malloc
是 C 标准库(以及 C++ 标准库)中的一个函数。
HeapAlloc
is a Windows API function.
HeapAlloc
是一个 Windows API 函数。
The latter lets you specify the heap to allocate from, which I imagine can be useful for avoiding serialization of allocation requests in different threads (note the HEAP_NO_SERIALIZE
flag).
后者允许您指定要从中分配的堆,我认为这对于避免不同线程中分配请求的序列化很有用(注意HEAP_NO_SERIALIZE
标志)。
回答by Roger Sumner
In systems where multiple DLLs may come and go (via LoadLibrary/Freelibrary), and when memory may be allocated within one DLL, but freed in another (see previous answer), HeapAlloc and related functions seem to be the least-common-denominator for successful memory sharing.
在多个 DLL 可能来来去去的系统中(通过 LoadLibrary/Freelibrary),并且当内存可能在一个 DLL 内分配,但在另一个 DLL 中释放时(见上一个答案),HeapAlloc 和相关函数似乎是最不常见的分母成功的内存共享。
Thread safe, presumably highly optimized by PhDs galore, HeapAlloc appears to work in all kinds of situations where our not-so-shareable code using malloc/free would fail.
HeapAlloc 线程安全,大概由博士们高度优化,似乎可以在我们使用 malloc/free 的不那么可共享的代码失败的各种情况下工作。
We are a C++ embedded shop, so we have overloaded operator new/delete across our system to use the HeapAlloc( GetProcessHeap( ) ) which can stubbed (on target) or native (to windows) for code portability.
我们是一家 C++ 嵌入式商店,因此我们在整个系统中重载了运算符 new/delete 以使用 HeapAlloc(GetProcessHeap()),它可以存根(在目标上)或本机(到 Windows)以实现代码可移植性。
So far no problems now that we have bypassed malloc/free which seem indisputably DLL specifically, a new "heap" for each DLL load.
到目前为止,没有问题,因为我们已经绕过了 malloc/free,这似乎是无可争议的 DLL,每个 DLL 加载都有一个新的“堆”。
回答by anonymous
Additionally, you can refer to:
此外,您可以参考:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366705(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366705(v=vs.85).aspx
Which stands that you can enable some features of the HEAP managed by WinApi memory allocator, eg "HeapEnableTerminationOnCorruption".
这意味着您可以启用由 WinApi 内存分配器管理的 HEAP 的某些功能,例如“HeapEnableTerminationOnCorruption”。
As I understand, it makes some basic heap overflow protections which may be considered as an added value to your application in terms of security.
据我了解,它提供了一些基本的堆溢出保护,在安全性方面可以将其视为应用程序的附加值。
(eg, I would prefer to crash my app (as an app owner) rather than execute arbitrary code)
(例如,我宁愿让我的应用程序崩溃(作为应用程序所有者)而不是执行任意代码)
Other thing is that it might be useful in early phase of development, so you could catch memory issues before going to the production.
另一件事是它在开发的早期阶段可能很有用,因此您可以在进入生产之前发现内存问题。
回答by rpk
mallocis exported function by C run-time library(CRT) which is compiler specific.
C run-time library dll name changes from visual studio versions to versions.
HeapAllocfunction is exported by kernel32.dll present in windows folder.
malloc是由特定于编译器的 C 运行时库 (CRT) 导出的函数。
C 运行时库 dll 名称从 Visual Studio 版本更改为版本。
HeapAlloc函数由 windows 文件夹中的 kernel32.dll 导出。
回答by Computer Software Guy
This is what MS has to say about it: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366533(v=vs.85).aspx
这就是 MS 不得不说的:http: //msdn.microsoft.com/en-us/library/windows/desktop/aa366533(v=vs.85).aspx
One thing one one mentioned thus far is: "The malloc function has the disadvantage of being run-time dependent. The new operator has the disadvantage of being compiler dependent and language dependent."
迄今为止有人提到的一件事是:“malloc 函数的缺点是依赖于运行时。新运算符的缺点是依赖于编译器和语言。”
Also, "HeapAlloc can be instructed to raise an exception if memory could not be allocated"
此外,“如果无法分配内存,可以指示 HeapAlloc 引发异常”
So if you want your program to run with any CRT, or perhaps no CRT at all, you'd use HeapAlloc. Perhaps only people who would do such thing would be malware writers. Another use might be if you are writing a very memory intensive application with specific memory allocation/usage patterns that you'd rather write your own heap allocator instead of using a CRT one.
因此,如果您希望您的程序与任何 CRT 一起运行,或者可能根本没有 CRT,您将使用 HeapAlloc。也许只有会做这种事的人才会是恶意软件编写者。另一种用途可能是,如果您正在编写具有特定内存分配/使用模式的内存密集型应用程序,您宁愿编写自己的堆分配器而不是使用 CRT 分配器。