Does the new operator we usually use use placement new to create objects in memory?

The function of

placement new is to perform the construction of objects on the memory allocated by operator new, so did new operator use placement new after using operator new to request memory? If not, how is it constructed? Does it have anything to do with placement new?

Mar.13,2021

< H2 > new is not an operator ! < / H2 >

In cPP, new and operator are both keywords . new int (x) is a (n) (new-) expression . operator new is a function. new operator in your title is new-expression indeed. new-expression will invoke oeprator new function .

The function of
placement new is to perform the construction of objects on the memory allocated by operator new,
Yes, that's true. To help you understand, here is a sample:
char* ptr = new char[sizeof(T)]; // ptr use a new-expression(newchar [sizeof(T)] to allocate memory, char is guaranteed to be sizeof 1
T* tptr = new(ptr) T;            // Here is placement new

In a nutshell, placement new will use already allocated memory to construct the object. Where is the already allocated memory from? Either from new expression ( free store ) or allocated from activation record like int buffer [10] , both ok.

so did new operator use placement new after using operator new to request memory? If not, how is it constructed? Does it have anything to do with placement new?

Above is my answer to your questions

BTW, from the case int buffer [10] , we can see pre- new-expression is not a must for placement new (however, note that placement new itself is a new-expression , which will invoke operator new function because all it does is just construct here). If your question is "will placement new always be after operator new / new-expression ", it will be a good question.

< H2 > Update < / H2 >

One year ago, I was confused about how to combine operator new with the constructor then asked a question, FrankHB answered my question: https://tieba.baidu.com/p/508. Now, look back to this question, it is a Xmuri Y question , What really confused me was how does new expression invoke constructor, so it is not related to placement new. Hope it will also inspire you.

< H2 > Update again < / H2 >
so I think maybe I'm similar to your question a year ago, how do the processes of memory request and constructor combine?
the word combination (combined with) is not properly now (I also make such mistake as said above), let me re-organize my wording:

new expression does two things:

  1. allocate memory
  2. initialization of the object

You and I (one year ago) are both confused about how does compiler initialize the object (via constructor or else) after allocating. Yes, I mentioned compiler , because CPP standard guarantee new will do the two things, but didn't how to, so, its compiler's work. So, it is not cPP's work, just compiler's. Now, we can see the assembly:

struct Foo
{
    Foo(int i) {}
};
int main()
{
    auto *a = new Foo(1);
}  

-O0:

Foo::Foo(int):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     DWORD PTR [rbp-12], esi
        pop     rbp
        ret
main:
        push    rbp
        mov     rbp, rsp
        push    rbx
        sub     rsp, 24
        mov     edi, 1
        call    operator new(unsigned long)
        mov     rbx, rax
        mov     esi, 1
        mov     rdi, rbx
        call    Foo::Foo(int)
        mov     QWORD PTR [rbp-24], rbx
        mov     eax, 0
        add     rsp, 24
        pop     rbx
        pop     rbp
        ret

codes related the new expression is follows: a

    mov     edi, 1
    call    operator new(unsigned long)
    mov     rbx, rax
    mov     esi, 1
    mov     rdi, rbx
    call    Foo::Foo(int)
    mov     QWORD PTR [rbp-24], rbx
    

Now, it is clear enough, right? Assemble calls two procedures, oeprator new and Foo::Foo (int)

< H2 > That's all, 's! < / H2 >

So, your question is how the two combined?


No, normal new does not initialize objects through placement new,new and placement new through constructors.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b311a3-34118.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b311a3-34118.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?