5

I'm trying to understand other person's code written in C++, but there is a strange use of constructor I've never seen. The code looks like this:

A* a = new A(some initial values...);
...
B* b = new (a) B(some initial values...);

When initializing variable b there is (a) between new and B(...). What does this mean?

enc
  • 3,345
  • 4
  • 21
  • 22

3 Answers3

7

The line of code:

B* b = new (a) B(some initial values...);

Is using a "placement new".

The default behavior; it is creating the new object of type B in the same memory location as the object a. If there are associated overloads for the placement new, then the behavior would be as coded in the overload, which could include some default type behavior as well.

The code needs to be considered with any overloads, memory layout of the objects and how the classes A and B relate to each other.

It is unusual to create an object over the location of a previously created object. I would imagine there is some code between these two presented here that deconstructs (but still leaves the memory "allocated") the previous object a before constructing the new one in its place.

The isocpp FAQ has some further advice on the use of this technique and its dangers.

Niall
  • 30,036
  • 10
  • 99
  • 142
  • 1
    then what happens to `a` ? how do they share the same location? – Selman Genç Jan 28 '16 at 12:50
  • 2
    @Selman22. Depends on what other code there is. Frankly I think the code smells. – Niall Jan 28 '16 at 12:51
  • 3
    It's not necessarily creating it at the same location, there could be an overload of `operator new(size_t, A*)` which does something completely different (e.g. calls `return a->allocate(sizeof(B))`) – Jonathan Wakely Jan 28 '16 at 12:52
  • @Selman22 the object pointed to by `a` ceases to exist. The pointer `a` probably can't be used (but that would depend on exactly what A and B were).. I would guess this code was an attempt to save on memory usage by re-using the space pointed to by `a` – M.M Jan 28 '16 at 12:53
  • @JonathanWakely. True that, the overloads introduce a number of alternatives. – Niall Jan 28 '16 at 12:53
  • I've looked into the code, there was overloaded `new` as @JonathanWakely said. – enc Jan 28 '16 at 13:04
  • I've seen the pattern used in pre-C++11 code for constructing new objects directly inside a container (remember, push_back copies the object inside the container). Basically, a weird emplace_back – KABoissonneault Jan 28 '16 at 13:57
2

This is so called placement new syntax.

Maybe you do not realize this but new behaves mostly as a standard C++ function and thus can be overridden and sometimes may also accept additional parameters. In this case, additional parameter (argument) being passed to the function new of class B is a.

You can read more on this for example here

Zegar
  • 1,005
  • 10
  • 18
0

new expression

Unless overloaded (in which case it could even format your hard drive), it means construct B at location a, without allocating new memory for its own sake.

Yam Marcovic
  • 7,953
  • 1
  • 28
  • 38
  • 1
    That's what the default placement-new operator does, but this might use an overloaded `operator new(size_t, A*)` that could return some completely different memory location, rather than the same location as `a`. – Jonathan Wakely Jan 28 '16 at 12:53