title: Allocating memory without initializing it in C++
body: <p>I'm getting acquainted with C++, and I'm having a problem with memory management. In C, whenever I'd want to reserve memory for any number of elements, regardless of type, I would just call <code>malloc()</code> and then initialize by hand (through a loop), to whichever value I wanted. With C++'s <code>new</code>, everything is automagically initialized.</p>
  
  <p>Problem is, I've got a BattlePoint class which goes a little something like this:</p>
  
  <pre><code>class BattlePoint {
  public:
      BattlePoint(int x, int y) : x(x), y(y) { };
      bool operator==(const BattlePoint &amp;right);
      virtual ~BattlePoint();
  
  private:
      int x, y;
  };
  </code></pre>
  
  <p>As you can see, it takes a few x and y values through the initializer and then sets its own x and y from it. The problem is, this function will be called from a function which will allocate an array of them:</p>
  
  <pre><code>BattleShip::BattleShip(BattlePoint start, enum shipTypeSize size, enum shipOrientation orientation) : size(size), orientation(orientation) {
      points = new BattlePoint[size]; // Here be doubts.
  }
  </code></pre>
  
  <p>So, I need my BattleShip's point to hold an array of BattlePoints, each one with different initialization values (such as 0,1; 0,2; 0,3, etcetera).</p>
  
  <p>Question is: how could I allocate my memory uninitialized?</p>
  
  <p>Julian,</p>
  
  <p>P.S.: I haven't done any testing regarding the way <code>new</code> works, I simple read <a href="http://en.wikipedia.org/wiki/New_%28C++%29" rel="nofollow">Wikipedia's article on it</a> which says:</p>
  
  <blockquote>
    <p>In the C++ programming language, as well as in many C++-based
    languages, new is a language construct that dynamically allocates
    memory on the heap and <strong>initialises the memory using the
    constructor</strong>. Except for a form called the "placement new", new
    attempts to allocate enough memory on the heap for the new data. If
    successful, it initialises the memory and returns the address to the
    newly allocated and initialised memory. However if new cannot allocate
    memory on the heap it will throw an exception of type std::bad_alloc.
    This removes the need to explicitly check the result of an allocation.
    A call to delete, which calls the destructor and returns the memory
    allocated by new back to the heap, must be made for every call to new
    to avoid a memory leak.</p>
  </blockquote>
  
  <p>placement new should be the solution, yet it makes no mention on how to do it.</p>
  
  <p>P.S. 2: I know this can be done through stdlib's vector class, but I'm avoiding it on purpose.</p>