0

I got this snippet of code and if you see in the return statement of NewNode after new in bracket i have buff. What does this buff in bracket is doing in new statement.

 struct Node
    { int key; }

    Node* NewNode(int key)
    {char *buff = allocate();
    ...
    return new (buff) Node(key)}
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Aman
  • 339
  • 3
  • 12
  • 2
    This is a special case of construction. Normally, the `new` is responsible to allocate memory _and_ call the constructor according to object type (in your case `NewNode`). In your case, the allocation is done prior. To consider this, [_placement `new`_](http://en.cppreference.com/w/cpp/language/new#Placement_new) is used which does only call of constructor using the given address `buff`. – Scheff's Cat May 06 '18 at 15:25
  • @user007 Based on the reasonable answer, this is a duplicate. Concerning the title and text, the question asks for it without using the term - it's hard to search for "placement new" if you don't know that/what it is. ;-) – Scheff's Cat May 06 '18 at 15:29
  • I rest my case i never knew about the term "placement new" in C++ thanks – Aman May 06 '18 at 15:36

0 Answers0