Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="https://stackoverflow.com/questions/7443782/does-dynamic-memory-allocation-differ-in-c-and-c-in-popular-implementations">duplicate</a> in qwr's comment has an <a href="https://stackoverflow.com/a/7447760/1508519">answer</a> that shows a snippet from Visual C++ (not MingW, but it's still relevant if you're on Windows):</p> <pre><code>#include &lt;cstdlib&gt; #include &lt;new&gt; _C_LIB_DECL int __cdecl _callnewh(size_t size) _THROW1(_STD bad_alloc); _END_C_LIB_DECL void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc) { // try to allocate size bytes void *p; while ((p = malloc(size)) == 0) if (_callnewh(size) == 0) { // report no memory static const std::bad_alloc nomem; _RAISE(nomem); } return (p); </code></pre> <p>As you can see, it's very similar to the gcc snippet.</p> <ol> <li><a href="https://stackoverflow.com/questions/3358045/how-are-malloc-and-free-implemented">These</a> </li> <li><a href="https://stackoverflow.com/questions/3199407/windows-memory-allocation-questions">links</a> </li> <li><a href="http://www.abstraction.net/ViewArticle.aspx?articleID=68" rel="nofollow noreferrer">may</a> </li> <li><a href="https://stackoverflow.com/questions/8224347/malloc-vs-heapalloc">be</a></li> <li><a href="https://stackoverflow.com/questions/18033518/what-are-the-windows-and-linux-native-os-system-calls-made-from-malloc">useful</a></li> </ol> <p>1 In OP's question:</p> <blockquote> <p>For Windows, I have found the functions: GetProcessHeap, HeapAlloc, HeapCreate, HeapDestroy and HeapFree.</p> </blockquote> <p>4</p> <blockquote> <p>You are right that they both allocate memory from a heap. But there are differences:</p> <pre><code>malloc() is portable, part of the standard. HeapAlloc() is not portable, it's a Windows API function. </code></pre> <p>It's quite possible that, on Windows, malloc would be implemented on top of HeapAlloc. I would expect malloc to be faster than HeapAlloc.</p> <p>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.</p> <p>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!</p> </blockquote> <p>5</p> <blockquote> <p>In Windows, in recent versions of MSVC, malloc (and C++ new, as it is implemented using the same fundamentals for the actual memory allocation part of new) calls HeapAlloc(). In other versions, such as g++ mingw, the C runtime is an older version, which doesn't call quite as directly to HeapAlloc, but at the base of it, it still goes to HeapAlloc - to find something different, we need to go back to Windows pre-95, which did have a GlobalAlloc and LocalAlloc set of functions - but I don't think people use 16-bit compilers these days - at least not for Windows programming.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload