Under the hood of the new

I discussed the answer of the doron to this question, and it was said that

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

I wonder if this is true. C ++ standard defines new to use malloc ?

+4
source share
4 answers

Does the C ++ standard define a new malloc for use?

The C ++ standard (which means the standard document that defines the C ++ language) does not define operator new in terms of malloc() . However, it is likely that in most implementations of the C ++ standard library, malloc() used to allocate memory.

In paragraph 18.6.1.1/4 of the C ++ 11 standard (near operator new ):

Default behavior

  • Loops: inside the loop, the function first tries to allocate the requested storage. If the attempt involves a function call to the standard C malloc , it is not specified .
+7
source

Does the C ++ standard define a new malloc for use?

No, although many implementations do this for the default implementation of new (it makes no sense to allocate separate heaps for malloc and new ).

In addition, the distribution function used by new is configurable (see section 3.7.4.1), so it is especially wrong to assume that new' will always call malloc` even in this implementation.

+2
source

No, malloc just allocates memory. But new allocates memory and call constructors (if necessary).

MAYBE in some part of the new implementation you will find a malloc -like mechanism as a memory allocator.

0
source

I don’t think so, since malloc does not know about constructors, and you need a little more than declaring a piece of memory

-1
source

Source: https://habr.com/ru/post/1468806/


All Articles