Search code examples
c++arraysplacement-new

Should I take the address of an array when using placement new?


If I have an array that's being used as a buffer for placement new:

alignas(T) unsigned char buf[sizeof(T)];

Then both uses of placement new will put a T into the buffer:

new (buf) T;
new (&buf) T;

But I take the address of the array in the second example, making a pointer to the entire array instead of a pointer to the first element. Which usage is correct?


Solution

  • Which usage is correct?

    Both are correct. The address of the array, and the address of the 1st element of the array, are guaranteed to be the same address. Placement-new doesn't care how you get that address.