So when you call malloc or new [] from your C/C++ application, how does the CRT translate it into Windows API calls?

  • 3
    See this question: stackoverflow.com/questions/6530355/… – Kilian Foth Aug 18 '12 at 7:33
  • new and malloc are not implemented in Windows. Well, there is a MSVCRT.DLL system library, but applications aren't supposed to use it; it's for internal use, and not a core DLL like user32.dll. A programming language targetting Windows must provide its own memory management based on the Win32 VirtualAlloc or HeapAlloc. Microsoft Visual C/C++ provides a "redistributable run-time" which provides malloc and operator new. If you ship a Visual C++ application, you have to ship this run-time with it. – Kaz Jul 4 '16 at 19:27

It depends if you are in debug or release mode. In release mode, as Pedro said there is HeapAlloc/HeapFree which are kernel functions,

while in debug mode (with visual studio) there is a hand written version of free and malloc (to which new/delete are re-directed) with thread locks and more exceptions detection, so that you can detect more easily when you did some mistakes with you heap pointers when running your code in debug mode. This is one more reason why a code compiled in debug mode is slower.

I think it is the only one API which is "simulated" like this in debug mode.

Just step it into the debugger to understand how it works, if I remember well this debug version uses somes trees of double linked lists.

And this is also with this kind of re-writing of malloc/free that memory consumption analysers work : there is tool for that in visual studio which just uses another .dll as the implementation of allocations functions which notice another data analyser program each time they are called.


Windows has a HeapAlloc function in the win32 API, maybe it's used.

The major point is that this is not so much related to Windows as it is to your C or C++ implementation. They implement malloc and how new works. So you should go about finding out how malloc and new are implemented in, for example, Visual C++.

I am not a C++ expert, but iirc, you can overload new; so there may be more going on there.

Not the answer you're looking for? Browse other questions tagged or ask your own question.