This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 116a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-12-19


1997. Placement new and previous initialization

Section: 6.7.5  [basic.indet]     Status: DRWP     Submitter: Jason Merrill     Date: 2014-09-08

Given the following example,

  #include <new>

  int main() {
    unsigned char buf[sizeof(int)] = {};
    int *ip = new (buf) int;
    return *ip; // 0 or undefined?
  }

Should the preceding initializsation of the buffer carry over to the value of *ip? According to 6.7.5 [basic.indet] paragraph 1,

When storage for an object with automatic or dynamic storage duration is obtained, the object has an indeterminate value, and if no initialization is performed for the object, that object retains an indeterminate value until that value is replaced (7.6.19 [expr.ass]).

In this case, no new storage is being obtained for the int object created by the new-expression.

Additional notes (August, 2024)

According to the resolution of issue 2721, storage for the elements of buf is considered reused and thus the lifetime of those elements ends when the allocation function returns. Out-of-lifetime objects cannot hold values.