views:

286

answers:

1

If I initialize a POD class with placement new, can I assume that the memory will be default initialized (to zeros)? This resource clearly states that if you call the zero argument default constructor explicitly that the fields will be default initialized, but it is not clear if this would hold true using the default implementation of placement new (passing an address). Is it standard behavior for the memory to be zeroed in this case?

+1  A: 

From the beginning:

5.3.4/15:

  • If the new-initializer is of the form (), then the item is value-initialized (8.5);

8.5/5:

To value-initialize an object of type T means:

  • if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

  • if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;

  • if T is an array type, then each element is value-initialized;

  • otherwise, the object is zero-initialized

The first bullet doesn't apply here because you have a POD type and therefore you cannot have a user declared constructor. The members of your type can only be POD types such as int, float etc or a nested structure that is also a POD or an array of POD types. So eventually each of them ends up at the final bullet: "otherwise, the object is zero-initialized".

This initialization occurs because the POD class is initialized in the memory that you've supplied. It doesn't matter that the memory was allocated by the OS or that you supplied an address.

So, IMHO, the answer is yes - the members will be zero initialized.

Richard Corden