title: Using placement new in a container
body: <p>I just came across some container implementation in C++. That class uses an internal buffer to manage its objects. This is a <strong>simplified version without safety checks</strong>:</p>
  
  <pre><code>template &lt;typename E&gt; class Container
  {
  public:
     Container() : buffer(new E[100]), size(0) {}
     ~Container() { delete [] buffer; }
  
     void Add() { buffer[size] = E(); size++; }
     void Remove() { size--; buffer[size].~E(); }
  
  private:
     E* buffer;
     int size;
  };
  </code></pre>
  
  <p>AFAIK this will construct/destruct <code>E</code> objects redundantly in <code>Container()</code> and <code>~Container()</code> if <code>new</code>/<code>delete</code> are not customized. This seems dangerous.</p>
  
  <p>Is using placement <code>new</code> in <code>Add()</code> the best way to prevent dangerous redundant constructor / destructor calls (apart from binding the class to a fully featured pool)?</p>
  
  <p>When using placement <code>new</code>, would <code>new char[sizeof(E)*100]</code> be the correct way for allocating the buffer?</p>