title: Can placement new for arrays be used in a portable way?
body: <p>Is it possible to actually make use of placement new in portable code when using it for arrays?</p>
  
  <p>It appears that the pointer you get back from new[] is not always the same as the address you pass in (5.3.4, note 12 in the standard seems to confirm that this is correct), but I don't see how you can allocate a buffer for the array to go in if this is the case.</p>
  
  <p>The following example shows the problem. Compiled with Visual Studio, this example results in memory corruption:</p>
  
  <pre><code>#include &lt;new&gt;
  #include &lt;stdio.h&gt;
  
  class A
  {
      public:
  
      A() : data(0) {}
      virtual ~A() {}
      int data;
  };
  
  int main()
  {
      const int NUMELEMENTS=20;
  
      char *pBuffer = new char[NUMELEMENTS*sizeof(A)];
      A *pA = new(pBuffer) A[NUMELEMENTS];
  
      // With VC++, pA will be four bytes higher than pBuffer
      printf("Buffer address: %x, Array address: %x\n", pBuffer, pA);
  
      // Debug runtime will assert here due to heap corruption
      delete[] pBuffer;
  
      return 0;
  }
  </code></pre>
  
  <p>Looking at the memory, the compiler seems to be using the first four bytes of the buffer to store a count of the number of items in it. This means that because the buffer is only <code>sizeof(A)*NUMELEMENTS</code> big, the last element in the array is written into unallocated heap.</p>
  
  <p>So the question is can you find out how much additional overhead your implementation wants in order to use placement new[] safely? Ideally, I need a technique that's portable between different compilers. Note that, at least in VC's case, the overhead seems to differ for different classes. For instance, if I remove the virtual destructor in the example, the address returned from new[] is the same as the address I pass in.</p>