Malloc () vs HeapAlloc ()

What is the difference between malloc () and HeapAlloc ()? As far as I understand, malloc allocates memory from a heap, like HeapAlloc, right?

So what is the difference?

Thank!

+40
c ++ malloc winapi
Nov 22 '11 at 9:14
source share
6 answers

You are right that both of them allocate memory from a heap. But there are differences:

  • malloc() is portable, part of the standard.
  • HeapAlloc() not portable, it is a Windows API function.

It is possible that on Windows malloc will be implemented on top of HeapAlloc . I would expect malloc be faster than HeapAlloc .

HeapAlloc has more flexibility than malloc . In particular, it allows you to specify which heap you want to allocate. This caters for several heaps per process.

For almost all coding scenarios, you should use malloc , not HeapAlloc . Although, since you noted your C ++ question, I expect you to use new !

+38
Nov 22 '11 at 9:17
source share

In fact, the malloc () functions (and other C runtime heap functions) are module dependent, which means that if you call malloc () in code from a single module (i.e. DLL), then you must call free () in the code in the code is the same module, or you could suffer some pretty bad heap damage (and this was well documented). Using HeapAlloc () with GetProcessHeap () instead of malloc (), including overloading new and deleted statements to use them, you can transfer dynamically distributed objects between modules and not worry about memory corruption if memory is allocated in the code by one module and freed in code another module when the pointer to the memory block is passed to the external module.

+59
Jan 04 '12 at 4:28
source share

With Visual C ++, the malloc() function or the new operator ultimately calls HeapAlloc() . If you are debugging the code, you will find that the _heap_alloc_base() function (in the malloc.c file) calls return HeapAlloc(_crtheap, 0, size) , where _crtheap is the global heap created using HeapCreate() .

The HeapAlloc() function does a good job of minimizing memory overhead, while allocating at least 8 bytes. The largest I've seen is 15 bytes per allocation, for allocations from 1 byte to 100,000 bytes. Larger blocks have a large overhead, however, since the percentage of the total allocated resources remains less than 2.5% of the payload.

I cannot comment on performance because I have not tested HeapAlloc() with a custom procedure, however, since the memory overhead when using HeapAlloc() , the overhead is surprisingly low.

+22
Apr 11 2018-12-12T00:
source share

malloc is a function in the C standard library (and also in the C ++ standard library).

HeapAlloc is a feature of the Windows API.

The latter allows you to specify a heap for allocation from it, which, in my opinion, can be useful for preventing serialization of distribution requests in different threads (pay attention to the HEAP_NO_SERIALIZE flag).

Cheers and hth.,

+6
Nov 22 '11 at 9:19
source share

Alternatively, you can refer to:

https://msdn.microsoft.com/en-us/library/windows/desktop/aa366705(v=vs.85).aspx

Which means that you can enable some of the HEAP functions that the WinApi memory allocator controls, for example, "HeapEnableTerminationOnCorruption".

As I understand it, it creates some basic heap overflow protections that can be considered as added value for your application from a security point of view.

(for example, I would rather crash my application (as the owner of the application) instead of executing arbitrary code)

Another thing is that it can be useful at an early stage of development, so you can catch memory problems before moving on to production.

+1
Mar 24 '17 at 8:19
source share

This is what MS has to say about it: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366533(v=vs.85).aspx

One thing that was mentioned above is this: "The malloc function has a drawback depending on the execution time. The new statement has a drawback that it depends on the compiler and depends on the language."

In addition, "HeapAlloc may be instructed to raise an exception if memory cannot be allocated."

So, if you want your program to work with any CRT or, possibly, without CRT, you would use HeapAlloc. Perhaps only the people who will do such things will be malware writers. Another use may be if you are writing an intensive memory application with specific memory allocation / usage patterns that you would instead write your own heap allocator instead of using CRT.

0
Oct 19 '12 at 16:14
source share



All Articles