8

what is the meaning for &t for expression new (&t) T(t) in c++?

as titled.

T t;
new (&t) T(t);
jiafu
  • 6,338
  • 12
  • 49
  • 73

3 Answers3

4

This is referred to as the 'placement new' syntax. The T value is constructed in the address that is specified by &t.

This sample is a bit off since it's creating a new T in the exact location of an existing T using the copy constructor. I think it's easier to explain this concept with an explicit address. Here is a variation of this code.

T t;
void* pAddress = malloc(sizeof(T));
new (pAddress) T(t);

// Or just creating a T without a copy ctor
new (pAddress) T();
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • It should be pointed out also that ``new (pAddress) T;`` is equally valid, ``T(t)`` instead of ``T`` is purely for calling the copy constructor instead of default constructor. – Diederick C. Niehorster Jan 07 '14 at 05:58
  • I am curious, does the OPs exact example actually work in practice? Shouldn't the destructor of the original ``T`` at ``t`` be called at some point? But when? Either its called before the copy construction, which means you can no longer copy, or its called after but then object is already overwritten... – Diederick C. Niehorster Jan 07 '14 at 05:59
  • @DiederickC.Niehorster Looks like the destructor is not called: http://ideone.com/lf3fx1 – tecu Jan 07 '14 at 06:01
  • @DiederickC.Niehorster: It should be called after. However, if the destructor is non-trivial, then it is undefined behavior, and all bets are off. – Benjamin Lindley Jan 07 '14 at 06:02
  • @tecu: In your example, the destructor of the local object `test` is called. Although it's undefined behavior, it is pretty easy to understand what is happening in this case. You called the constructor on top of the memory that was allocated for `test`, overwriting its value. – Benjamin Lindley Jan 07 '14 at 06:06
  • @BenjaminLindley: I am failing to see why this would be UB. You are only replacing the content of the object but the object ownership is not transferred. The object would be destroyed during stack unwinding in the same way he object would have been destroyed without using placement new. – Abhijit Jan 07 '14 at 06:10
  • JaredPar, you should also note that `pAddress` may have alignment issues! – Nawaz Jan 07 '14 at 06:41
  • @Abhijit: I looked into it, and you are right. I could have sworn I read before that it was UB, but it only is if the original object is `const`. – Benjamin Lindley Jan 07 '14 at 06:44
  • @BenjaminLindley: Off-course, and that's a separate question. – Abhijit Jan 07 '14 at 06:47
  • @Nawaz: `malloc` is guaranteed to return an address suitably aligned for any object. However, that guarantee is for C structs. I don't know if it applies to C++ non-POD classes. – Siyuan Ren Jan 07 '14 at 07:46
  • @C.R.: `malloc` doesn't know anything about `T`, hence it cannot know the alignment of `T`, right? If so, how can it return memory with correct alignment? – Nawaz Jan 07 '14 at 07:48
  • 1
    @Nawaz: It returns the address with maximum alignment. A pointer aligned to 16 byte boundary is also aligned to 4 byte boundary, for example. – Siyuan Ren Jan 07 '14 at 08:08
  • @C.R.: Ohh.. I didn't know that. Could you give me the reference? – Nawaz Jan 07 '14 at 08:31
  • @Nawaz: I don't have C standards documents. But here is something related: http://stackoverflow.com/questions/8752546/how-does-malloc-understand-alignment – Siyuan Ren Jan 07 '14 at 08:52
2

This is an example of placement new, where the container is the starting address of the object t created with the Type T

The example code creates an Object of Type T using the copy constructor which accepts a parameter of Type T and places the object in the memory location of the object t.

Just to conceive the Idea, consider the Type as an integer, so your template code would look like

int t;
new (&t) int(t);

So apparently, the placement new is redundant and confusing. The only reason for writing such an odd code may be is to force the initialization of an object using Copy Constructor which might have some additional code apart from copying the contents of the object which user might be interested.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
2

This is the Placement Syntax; This statment:

new (&t) T(t);

Constructs an object at the adress of t (&t) but it doesn't allocate memory.

rullof
  • 7,124
  • 6
  • 27
  • 36