As far as I know a placement new
doesn't allocate memory but instead it is a form of new
operator that takes a pointer to an already allocated memory and construct an object there.
But I am so confused about this text from C++ primer 5th edition:
"Instead, we use the placement
new
form ofnew
(§ 12.1.2, p. 460) to construct an object. As we’ve seen, this form ofnew
provides extra information to the allocation function. We can use placementnew
to pass an address, in which case the placement new expression has the form
new (place_address) type
new (place_address) type (initializers)
new (place_address) type [size]
new (place_address) type [size] { braced initializer list }
where place_address must be a pointer and the initializers provide (a possibly empty) comma-separated list of initializers to use to construct the newly allocated object.
When called with an address and no other arguments, placement new uses operator new(size_t, void*) to “allocate” its memory. This is the version of operator new that we are not allowed to redefine (§ 19.1.1, p. 822). This function does not allocate any memory; it simply returns its pointer argument. The overall new expression then finishes its work by initializing an object at the given address. In effect, placement new allows us to construct an object at a specific, preallocated memory address.
Note
When passed a single argument that is a pointer, a placement new expression constructs an object but does not allocate memory."
What does it mean "When called with an address and no other arguments, placement new uses operator new(size_t, void*) to “allocate” its memory."? Does it mean placement new calls a function to allocate memory?
- Also the note says that when placement new is passed a single argument that is a pointer, it constructs an object but doesn't allocate memory: Does this mean if passed more than one argument it can allocate memory?
Can someone clear up this to me? Thank you!