18

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 than n, and if T isn't a character type then new T[n] is only required to return an address aligned for T.

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.

Community
  • 1
  • 1
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Similar question is: Is it possible to free pointer allocated with `new` keyword with `free()` instead of `delete`. – i486 Jan 15 '16 at 14:41

3 Answers3

27

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.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
5

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)

David Haim
  • 25,446
  • 3
  • 44
  • 78
2

Yes, it may call malloc - under windows with VS and standard runtime library it does call malloc.

You are allowed to overload new operator and call your own allocation function. In application I work on, we have custom malloc from Doug Lea with lots of customizations for embeded systems. Windows calls malloc because it calls HeapAlloc, which is standard heap allocation function under windows. It also allows debugging allocation errors with CrtDbg api.

To make answer more formal I have looked up the standard and in §18.6.1.1, I found that new

Executes a loop: Within the loop, the function first attempts to allocate the requested storage. Whether the attempt involves a call to the Standard C library function malloc is unspecified.

so wheteher malloc is used is unspecified - it might use it or not.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • And _must_ it call malloc? – einpoklum Jan 15 '16 at 14:46
  • 1
    @einpoklum, of course not. It is implementation detail. Moreover, the answer is not technically correct - it matters what standard library you use, not what compiler or OS. However, it would make sense for any library implementor to reuse already available functionality of malloc(), rather than do your own OS-specific coding. – SergeyA Jan 15 '16 at 14:47
  • @SergeyA: Technically, the standard talks about "an implementation". If you build your own implementation by taking a compiler from one place and a standard library from another and a linker from a third, then you are an implementor and responsible for getting it right. Most people get their implementation from a single source (and call it "a compiler"). – Martin Bonner supports Monica Jan 15 '16 at 15:22
  • @MartinBonner, which is certainly not a technically correct term. For example, CLang as a compiler ships without std library (while providing one independently), and Apache provides a library without compiler. – SergeyA Jan 15 '16 at 16:01
  • What is "not a technically correct term"? "An implementation"? The first sentence of the standard is "This International Standard specifies requirements for implementations of the C++ programming language." The library is part of that implementation. – Martin Bonner supports Monica Jan 15 '16 at 16:10
  • @MartinBonner, of course not. A 'compiler', as stated in your comment, is not a correct term to to be used when 'implementation' is meant. I feel we are splitting hairs, though. – SergeyA Jan 15 '16 at 18:32