title: Cannot allocate memory for a 2 dimentional array of pointers c++
body: <p>So I'm trying to create a 2 dimensional array of pointers to a object of type Piece. The problem is when i try assign a pointer to a piece to the array i get a segmentation fault. I realized I needed to initialize to array to sometime before I can start allocating but I can't get it right.</p>
  
  <p>Here is the header file of Map which contains a 2-d array of pointers.</p>
  
  <pre><code>#ifndef MAP_H
   #define MAP_H
  
   #include &lt;iostream&gt;
   #include &lt;vector&gt;
   #include &lt;fstream&gt;
   #include &lt;stdio.h&gt;
   #include &lt;stdlib.h&gt;
   #include &lt;sstream&gt;
   #include &lt;string&gt;
   #include &lt;cstring&gt;
   #include "Player.h"
   #include "Sprite.h"
   #include "Piece.h"
   #include "Messages.h"
   #include "PieceType.h" 
  
  using namespace std;
  
  class Map
  {
      private:
  
          Piece*** pieces;
          int startingX;
          int startingY;
          int width;
          int height;
          string mapName;
  
      public:
  
          Map(string);
          ~Map();
  
          void printMap() const;
          Piece* pieceType(char);
          void setSprite(Piece*);
          void firstMove();
          void resetMap(string);
  
          bool moveUp(int, int);
          bool moveDown(int, int);
          bool moveLeft(int, int);
          bool moveRight(int, int);
  
          int getHeight();
          int getWidth();
  
  
  };
  
  #endif
  </code></pre>
  
  <p>The array I'm talking about is pieces.</p>
  
  <p>I try to allocate this in the constructor of Map.</p>
  
  <pre><code>Map::Map(string name)
  {
    ifstream map;
    string line;
    string dimention;
    mapName = name;
  
    map.open(name.c_str());
  
    if (map.good())
    {
      getline (map, line);
  
      int i = 0;
  
      while(line[i] != 'X')
      {
        dimention[i] = line[i];
        i++;
      }
  
      stringstream convert(dimention);
  
      convert &gt;&gt; width;
  
      int temp = i;
      dimention = "";
      i = 1;
  
      while(line[(i + temp)] != '\0')
      {
        dimention[i] = line[(i + temp)];
        i++;
      }
  
      stringstream convertTwo(dimention);
  
      convertTwo &gt;&gt; height;
  
      for (int i = 0; i &lt; height; i++)
       {
         if (!(map.eof()))
         { 
       getline (map, line);
         }
         else
         {
       cout &lt;&lt; "Error with file" &lt;&lt; endl;
       break;
         }
  
         for (int j = 0; j &lt; width; j++)
         {
       pieces[i][j] = pieceType(line[j]); //This is where I'm getting the segmentation fault
  
       cout &lt;&lt; "assigned" &lt;&lt; endl;
  
       if ((pieces[i][j])-&gt;getType() == WAYPOINT)
       {
  
         if (pieces[i][j]-&gt;getWaypointType() == 0)
         {
           startingX = j;
           startingY = i;
         }
       }
  
       else
       {       
       (pieces[i][j])-&gt;setXCordinate(j);
       (pieces[i][j])-&gt;setYCordinate(i);
       }
  
         }
       }
    }
  }
  </code></pre>
  
  <p>Where name is a string that holds the name of the file that has the information for loading a particular map.</p>
  
  <p>Also the function pieceType is as follows:</p>
  
  <pre><code>Piece* Map::pieceType(char type)
  {
    Piece* temp;
  
    if (type == '.')
    {
      return NULL;
    }
    if (type == 'S')
    {
      temp = new Waypoint(0);
      return temp;
    }
    if (type == 'E')
    {
      temp = new Waypoint(1);
      return temp;
    }
  }
  </code></pre>
  
  <p>Waypoint is a derived class of Piece.</p>