2

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 of new (§ 12.1.2, p. 460) to construct an object. As we’ve seen, this form of new provides extra information to the allocation function. We can use placement new 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!

Itachi Uchiwa
  • 3,044
  • 12
  • 26
  • 3
    Second sentence after your quote: This function does not allocate any memory; – NathanOliver Jun 08 '21 at 20:18
  • @NathanOliver: But does it call operator new? and what does exactly? – Itachi Uchiwa Jun 08 '21 at 20:23
  • 1
    See (9) here https://en.cppreference.com/w/cpp/memory/new/operator_new – Richard Critten Jun 08 '21 at 20:24
  • 1
    *"When called with an address and no other arguments, placement new uses operator new(size_t, void*) to “allocate” its memory."*, So yes. *This function does not allocate any memory; it simply returns its pointer argument.* So basically, nothing. – NathanOliver Jun 08 '21 at 20:25
  • 1
    @SilvioMayolo Well, that's the first book listed under [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) :D – Ranoiaetep Jun 08 '21 at 20:27
  • @NathanOliver: Is placement new is a sort of new expression rather than operator new? and does it mean that placement new calls operator new too like new expression and why? – Itachi Uchiwa Jun 08 '21 at 20:44
  • 1
    @ItachiUchiwa Placement new is the C++ way to say: Hey, construct an object of the type I specify in the location that the provided pointer points to. It leaves it up to you to already have the required space allocated. – NathanOliver Jun 08 '21 at 20:47
  • 1
    @ItachiUchiwa Give this a read, should help clear this all up: https://stackoverflow.com/questions/35087204/how-c-placement-new-works – NathanOliver Jun 08 '21 at 20:53

1 Answers1

0

The problem, I think, is a new expression does two things:

  1. allocates memory via operator new
  2. invokes the constructor of the object in that allocated memory

Now, normally, allocating memory eventually calls malloc or asks the OS for memory in some way. However, with placement new, you're "going manual" for step 1, and instead of an allocation function being called to provide the memory, you are passing in a pointer that you promise is a valid place to construct the object. It'll just use the memory you give it to create the object in the place you say to make it.

Chris Uzdavinis
  • 6,022
  • 9
  • 16