title: Overhead of placement new[]
body: <p>Current draft standard explicitly <a href="http://eel.is/c++draft/expr.new#15" rel="noreferrer">states</a> that placement <code>new[]</code> can have a space overhead:</p>
  
  <blockquote>
    <p>This overhead may be applied in all array new-expressions, including those referencing the library function operator new[](std​::​size_­t, void*) and other placement allocation functions. The amount of overhead may vary from one invocation of new to another.</p>
  </blockquote>
  
  <p>So presumably they have something in mind, why a compiler need this overhead. What is it? Can a compiler use this overhead for anything useful?</p>
  
  <p>In my understanding, to destruct this array, the only solution is to call destructors in a loop (am I right on this?), as there is no placement <code>delete[]</code> (btw, shouldn't we have placement <code>delete[]</code> to properly destruct the array, not just its elements?). So the compiler doesn't have to know the array length.</p>
  
  <p>I thought as this overhead cannot be used for anything useful, compilers don't use it (so this is not an issue in practice). I've checked compilers with this simple code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;new&gt;
  
  struct Foo {
      ~Foo() { }
  };
  
  int main() {
      char buffer1[1024];
      char buffer2[1024];
  
      float *fl = new(buffer1) float[3];
      Foo *foo = new(buffer2) Foo[3];
  
      printf("overhead for float[]: %d\n", (int)(reinterpret_cast&lt;char*&gt;(fl) - buffer1));
      printf("overhead for Foo[]  : %d\n", (int)(reinterpret_cast&lt;char*&gt;(foo) - buffer2));
  }
  </code></pre>
  
  <p>GCC and clang doesn't use any overhead at all. But, MSVC uses 8 bytes for the <code>Foo</code> case. For what purpose could MSVC use this overhead?</p>
  
  <hr>
  
  <p>Here's some background, why I put this question.</p>
  
  <p>There were previous questions about this subject:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8720425/array-placement-new-requires-unspecified-overhead-in-the-buffer">Array placement-new requires unspecified overhead in the buffer?</a></li>
  <li><a href="https://stackoverflow.com/questions/15254/can-placement-new-for-arrays-be-used-in-a-portable-way">Can placement new for arrays be used in a portable way?</a></li>
  </ul>
  
  <p>As far as I see, the moral of these questions is to avoid using placement <code>new[]</code>, and use placement <code>new</code> in a loop. But this solution doesn't create an array, but elements which are sitting next to each other, which is <strong>not an array</strong>, using <code>operator[]</code> is undefined behavior for them. These questions are more about how to avoid placement <code>new[]</code>, but this question is more about the "why?".</p>