title: UB when deleting storage-providing char array from free store?
body: <p>The following example of usage of placement-new was provided by an earlier version of the <a href="https://en.cppreference.com/w/Talk:cpp/language/new" rel="nofollow noreferrer">cppreference page</a>:</p>
  <pre><code>char* ptr = new char[sizeof(T)]; // allocate memory
  T* tptr = new(ptr) T;            // construct in allocated storage (&quot;place&quot;)
  tptr-&gt;~T();                      // destruct
  delete[] ptr;                    // deallocate memory
  </code></pre>
  <p>Inspired by <a href="https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new#comment2480300_222578">this comment on an older SO thread</a> I have come to the conclusion that this might be UB. However, in the talk page of that cppreference article there is a small discussion as to whether this is actually the case.</p>
  <p>The comment was based on this standard excerpt which might suggest that it could be UB:</p>
  <blockquote>
  <p>[expr.delete] 3
  In an array delete expression, if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.</p>
  </blockquote>
  <p>Does this apply even if the T's lifetime has ended?
  EDIT: As pointed out by Language Lawyer in the comments: the dynamic type of ptr remains unchanged, so this doesn't apply.</p>
  <p>On the other hand [basic.life] says that: (bold part addded by me)</p>
  <blockquote>
  <p>[...] after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any pointer that represents the address of the storage location where the object will be or was located may be used but only in limited ways.
  [...] such a pointer refers to allocated storage ([basic.stc.dynamic.allocation]), and using the pointer as if the pointer were of type void* is well-defined. <strong>(Please note: [exp.delete] notes that &quot;an object cannot be deleted using a pointer of type void*&quot;)</strong>
  Indirection through such a pointer is permitted but the resulting lvalue may only be used in limited ways, as described below.
  <em>The program has undefined behavior if</em>:</p>
  <p>-the object will be or was of a class type with a non-trivial destructor and the pointer is used as the operand of a delete-expression,</p>
  <p>...</p>
  </blockquote>
  <p>As the char[] is <em>not</em> a class type with a non-trivial destructor, this doesn't apply and neither do the other elements of the list of explicitly undefined behaviours of that paragraph. However, does that imply that it <em>is</em> otherwise well-defined? That specific bullet point seems to suggest that is is the possible destructor mismatch that would present a problem, which doesn't apply here.</p>
  <p>Also, the wording is &quot;before the storage which the object occupied is reused&quot;, but technically it has been reused, it's just that the object that reused it also doesn't live anymore. It could be possible that only a pointer to the object with the most recent life time in that storage is allowed to be used here.</p>
  <p>In conclusion: is it UB to delete a dynamically allocated char array after it has provided storage for another object that has since been destructed? If yes, is there a legal way to deallocate it at all, or should it be completely replaced with an explicit operator new / operator delete combination like how the standard allocator does it?</p>