Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Under the hood of new

I was discussing doron's answer to this question, and it was said that

[one] will probably find that the C++ standard library defines new to use malloc under the hood.

I'm wondering if this is true. Does the C++ Standard define new to use malloc?

like image 246
Michael Dorst Avatar asked Jan 13 '23 23:01

Michael Dorst


1 Answers

Does the C++ standard define new to use malloc?

The C++ Standard (meaning the Standard document that defines the C++ Language) doesn't define operator new in terms of malloc(). However, it is plausible that most implementations of the C++ Standard Library use malloc() to allocate memory.

Per Paragraph 18.6.1.1/4 of the C++11 Standard (about operator new):

Default behavior

  • 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.
like image 76
Andy Prowl Avatar answered Jan 18 '23 20:01

Andy Prowl