1

I've been reading some questions about placement new on SO and I am finding some different ways to use it that I thought I would ask about here. I understand that placement new is basically a way for you to create an object and give it a certain place in memory of your choice that has already been allocated; I just don't understand all of the intricacies.

First one I saw on SO uses this example (from C++ Primer):

const int BUF = 512;
const int N = 5;
char buffer[BUF];
double * pd1;
pd1 = new (buffer) double[N];

Second one I saw on SO uses this example (from C++ Primer Plus):

char * buffer = new char[BUF];   //get a block of memory
JustTesting *pc1, *pc2;

pc1 = new (buffer) JustTesting;  //Place object in buffer

The main difference between these two is that in the first block of code, buffer is a char array and in the second example it is a pointer to a char array, yet both blocks of code contain new (buffer).

My first question is: When using placement new in this context, can it accept either an object or it's address, and if so, is one preferred?

I've also seen placement new used without an lvalue. See below.

new(&myObject) myClass;

My second question is: What are the differences between placement new with vs. without an lvalue?

ImaginaryHuman072889
  • 4,953
  • 7
  • 19
  • 51
  • 7
    An array variable decays to a pointer to the first element, so although your two examples look different they're actually both using pointers. – Mark Ransom Sep 15 '17 at 16:40
  • 2
    You probably need a better understanding of the relationship between pointers and arrays - both examples are using pointers. –  Sep 15 '17 at 16:40
  • @Mark Ransom Oh, didn't realize it was that simple. Thanks, that answers my first question. – ImaginaryHuman072889 Sep 15 '17 at 16:42

2 Answers2

1

I understand that placement new is basically a way for you to create an object and give it a certain place in memory of your choice that has already been allocated;

Not exactly.

new expression calls the corresponding operator new and then invokes the constructor. Note that these are two different things named similarly.

Placement new expression skips allocating memory and only invokes the constructor. This is achieved by using the placement void* operator new( std::size_t count, void* ptr), which does nothing and returns ptr.

It is your job to provide storage for the object being initialized when using placement new.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • 1
    `new expression` and `operator new` are different yes. Might also mention that [`new operator`](https://stackoverflow.com/questions/1885849/difference-between-new-operator-and-operator-new) is a third separate thing. – Mooing Duck Sep 15 '17 at 16:51
  • @MooningDuck According to both my previous understanding and the accepted answer of that link, the `new` operator is the same thing as the `new` expression, which calls `operator new` and the constructor. – Daniel H Sep 15 '17 at 16:54
  • There is `new expression` that involves invoking the corresponding `operator new`. One can call `operator new` directly. Only 2 things here. – Maxim Egorushkin Sep 15 '17 at 16:56
  • @MaximEgorushkin Ok thanks for the explanation, but what is the difference between placement new with vs. without an lvalue? – ImaginaryHuman072889 Sep 15 '17 at 17:55
  • @Peter In both cases it calls `void* operator new( std::size_t count, void* ptr)`, hence no difference (unless there are user defined `operator new` overloads). – Maxim Egorushkin Sep 16 '17 at 18:44
0

Your first question is already answered. For your 2nd question - What are the differences between placement new with vs. without an lvalue?

Since placement new returns the address that is passed in its 2nd parameter, requiring an lvalue to store that address is optional. See the placement new implementation below from Microsoft Visual Studio-

inline void *__CRTDECL operator new(size_t, void *_Where) _THROW0()
    {   // construct array with placement at _Where
    return (_Where);
    }
Amit Rastogi
  • 926
  • 2
  • 12
  • 22