It can, and usually it does.
On Windows (more specificly on VC++), the chain of calls looks like
operator new
calls malloc
calls HeapAlloc
HeapAlloc
is a Windows API function in for allocating memory from specific heap.
when the process goes up, it allocate a heap (the CRT heap) in which all the standard allocation takes memory.
No, it isn't obligated to call malloc. it is up to the library developers/end user developer to decide from where they want their memory from.
For example, I can create a mono-threaded program. usually the heap allocator locks the heap lock when allocation/deallocation takes place, in order to prevent fatal race-condition on the heap.
but if my program is monothreaded, I don't have the problem.
I may choose to create my own heap with WinApi HeapCreate
and pass HEAP_NO_SERIALIZE
which makes the heap skip the lock. then I can use operator new
with plain HeapAlloc
. this is a case where I can make new
work with different function then malloc
.
Another low level approach which is sometimes* is done is to allocate huge memory block with VirtualAlloc
, then pass a re-calculated memory address anytime someone calls new
.
(all of these approches are done pretty rarely, and from my experiance they bring minimal improvment to the execution time)