Does :: operator new (size_t) use the malloc () command?

Does ::operator new(size_t) call the malloc() ::operator new(size_t) call internally or does it use OS-specific system / library calls? What does the C ++ standard say?

This answer says that:

malloc() guaranteed to return an address aligned to any standard type. ::operator new(n) guaranteed to return an address aligned for any standard type not exceeding n , and if T not a character type, then a new T[n] is only required to return an address aligned for T

And this suggests that new() cannot require a call to malloc() .

Note: there SO question about everything operator new does differently than distribution.

+7
c ++ new-operator malloc dynamic-memory-allocation
source share
3 answers

Implementation details operator new is a property of the particular implementation of the standard library โ€” not even for the compiler or operating system. I am familiar with one (gnu) and I know 3 others - CLang, Apache and MSFT. They all use malloc() inside operator new , because it just makes life easier for the library developer.

If malloc() not used, the developer would have to redefine a lot in terms of memory allocation and sprinkle the code heavily with OS-specific logic to actually query the memory. No one wants to do this when malloc() already exists. But they are in no way obligated to use it.

+8
source share

He can, and usually he does.
On Windows (more specifically on VC ++), the call chain looks like

operator new calls malloc calls HeapAlloc

HeapAlloc is a Windows API function for allocating memory from a specific heap. when the process goes up, it allocates a heap (a CRT heap) in which all the standard distribution takes memory.

No, it is not necessary to call malloc. library developers / end-user developers to decide where they want from their memory.

For example, I can create a single-threaded program. typically, the heap dispenser locks the heap lock during allocation / deallocation to prevent the fatal race condition on the heap. but if my program is monotested, I have no problem.
I can create my own heap using WinApi HeapCreate and pass HEAP_NO_SERIALIZE , which is why the heap skips locking. then I can use operator new with a simple HeapAlloc . this is the case when I can get new to work with another function and then malloc .

Another low-level approach that sometimes runs * is to allocate a huge block of memory using VirtualAlloc , and then pass the recalculated memory address anytime someone calls new .

(all of these actions are performed quite rarely, and from my experience they bring minimal improvement in execution time)

+3
source share

Yes, it can call malloc - under windows with VS and the standard runtime library, it calls malloc .

You are allowed to overload the new operator and call your own distribution function. In the application I'm working on, we have a custom malloc from Doug Lea with many settings for embedded systems. Windows calls malloc because it calls HeapAlloc, which is the standard heap allocation function under windows. It also allows debugging selection errors using CrtDbg api.

To make the answer more formal, I looked at the standard and in ยง18.6.1.1, I found that new

Loops: inside the loop, the function first tries to allocate the requested storage. Will an attempt to include a call to the library function Standard C malloc not specified .

therefore settele malloc is used, it is not indicated whether he can use it or not.

+2
source share

All Articles