Does ::operator new(size_t)
call malloc()
internally, or does it use system calls / OS-specific library calls directly? What does the C++ standard say?
In this answer it says that:
malloc()
is guaranteed to return an address aligned for any standard type.::operator new(n)
is only guaranteed to return an address aligned for any standard type no larger thann
, and ifT
isn't a character type then newT[n]
is only required to return an address aligned forT
.
And that suggests that new()
cannot be required to call malloc()
.
Note: There's an SO question about everything operator new
does other than allocation.
The details of how operator new
is implemented are property of a particular implementation of standard library - not even a compiler or operation system. I am familiar with one (gnu) and aware of 3 others - CLang, Apache and MSFT. All of them are using malloc()
within operator new
, because it just makes a life of library developer so much easier.
If malloc()
were not used, said developer would have to reimplement a lot in terms of memory allocation, and sprinkle the code heavily with OS-dependent logic to actually request memory. No one wants to do this when malloc()
is already there. But by no means they are obliged to use it.