Title
Placement new and previous initialization
Status
drwp
Section
6.7.5 [basic.indet]
Submitter
Jason Merrill

Created on 2014-09-08.00:00:00 last changed 4 months ago

Messages

Date: 2024-08-15.00:00:00

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.

Date: 2023-03-13.07:00:01

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.

History
Date User Action Args
2024-08-22 11:33:28adminsetmessages: + msg7807
2024-08-22 11:33:28adminsetstatus: drafting -> drwp
2014-09-08 00:00:00admincreate