4

This is enclosed in a for loop:

v[i] = new (&vv[i]) vertex(pts[i],i);
  • vertex is a struct
  • pts is a point*
  • v is a vertex**
  • vv is a vertex*

What does the (&vv[i]) part do?

Mat
  • 202,337
  • 40
  • 393
  • 406
ackerleytng
  • 416
  • 6
  • 17

4 Answers4

9

It looks like placement new. It's the same as an ordinary new statement, but instead of actually allocating memory, it uses memory already available and which is pointed to by the expression inside the parentheses.

In your case it uses the memory in vv[i] to create the new vertex object, then returns a pointer to that (i.e. &vv[i]) and it's assigned to v[i].

See e.g. this reference for more details.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

That's placement new-expression.

It's constructing a new object at already allocated memory - at the address of vv[i].

It's calling this allocation function:

operator new(std::size_t, void*); 
//                        ^^^^
//                        &vv[i] is passed here

which simply returns the second argument. The constructor of vertex that matches the number of arguments of their types is then called to construct the object in place.

jrok
  • 54,456
  • 9
  • 109
  • 141
1

Yes,it is a placement new, it sepcify a address where the new Object place in. if your (&vv[i]) equal 0xabcdef00, your new object will be in 0xabcdef00, more detial you could check c++ standard 5.3.4.

Chen
  • 119
  • 9
0

This is placement new that allows placing an object at a specified memory location. See http://www.parashift.com/c++-faq/placement-new.html and What uses are there for "placement new"?.

Community
  • 1
  • 1
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91