title: Global "placement" delete[]
body: <p>I am trying to replace new/delete with my own allocator(s). So, overriding placement new and delete - quite happy with that. Looks something like this ...</p>
  
  <pre><code>void* operator new( size_t size, Allocator* a )
  {
      return a-&gt;Alloc( size );
  }
  
  template&lt;class T&gt; inline void MyDelete( T* p, Allocator* a )
  {
      if( p )
      {
          p-&gt;~T();
          a-&gt;Free( p );
      }
  }
  </code></pre>
  
  <p>The C++ language specifies that, for placement delete, you have to explicitly call the ~dtor. The compiler doesn't do it for you. Whether this is a templatised operator delete or explicit function as shown.</p>
  
  <p>See <a href="http://www2.research.att.com/~bs/bs_faq2.html#placement-delete" rel="noreferrer">http://www2.research.att.com/~bs/bs_faq2.html#placement-delete</a></p>
  
  <p>The problem is - how can I get this to work for array delete[]? I know I need to iterate through the array and call ~dtor myself. Therefore I need the size of the array, </p>
  
  <p><em>Edited for clarity</em></p>
  
  <p>I can store this information or infer it from the block size. However, the problem is the compiler (MSVC v9) does different things if I am allocating an array of objects with destructors compared to ones without, i.e. if there is a dtor it will allocate an extra 4 bytes. This is because the compiler for standard delete[] needs to do the same thing and can pair up the appropriate code for delete[]. </p>
  
  <p>However in my own "placement" delete[] I have no way of knowing what the compiler did or determining safely at compile time if the class has a dtor. </p>
  
  <p>E.g.</p>
  
  <pre><code>char buf[ 1000 ];
  
  MyClass* pA = new( buf ) MyClass[ 5 ];
  </code></pre>
  
  <p>Here the value of pA is buf + 4 if there exists ~MyClass() and the amount of memory allocated is sizeof(MyClass) * 5 + 4. However if there is no dtor then pA == buf and the amount of memory allocated is sizeof(MyClass) * 5.</p>
  
  <p>So my question is - is this behaviour a language standard and consistent across compilers or is it a peculiar to MSVC? Has anyone else got a good solution to this problem? I guess the only option is to not use new[] and do the construction myself which is fine but then the calling code syntax is a little unusual .. or force every class to have a destructor.</p>