title: C++: Error while allocating memory for a two dimensional array using functions
body: <p>I am trying to understand passing by reference and passing by value. In this program I have a two dimensional array which is declared in main and whose size is allocated in a function. In the allocate2DArrayRef() function, I obtain the two sizes and dynamically allocate and initialize the array Array2D.</p>
  
  <p>Now I am trying to understand how to do the same by pointers. I have written another function  allocate2DArrayPtr() in where I pass a pointer to the two dimensional array, get the value of the sizes - sizeX and sizeY and then allocate the memory to the variable secArray2D.</p>
  
  <p>When I run the program it hangs when I try to print out secArray2D. I am assuming this implies that the function allocate2DArrayPtr() has not been successful in dynamically allocating memory to the array secArray2D.</p>
  
  <p>My final goal is to write a program which has a function that dynamically allocated memory and initializes multiple arrays of various dimensions which are read from an input file. I know I can expand on passing by reference function allocate2DArrayRef() to achieve my goal. But I am curious to learn why my function allocate2DArrayPtr() is not working as I want to be clear on how to pass by pointers also. I do know how to change allocate2DArrayPtr() to return a pointer but I would like to pass the array as a parameter.</p>
  
  <p>I am running the program on windows 7 using Codeblocks 13.12 IDE.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  void allocate2DArrayRef(int **&amp;, int &amp;, int &amp;);
  void allocate2DArrayPtr(int ***, int *, int *);
  
  int main()
  {
     int sizeX, sizeY;
     int **Array2D;
     allocate2DArrayRef(Array2D, sizeX, sizeY);
     for(int i=0; i&lt; sizeX; i++)
     {
        for(int j=0; j&lt; sizeY; j++)
        {
           cout &lt;&lt; "Array2D[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "]:" &lt;&lt; Array2D[i][j] &lt;&lt;     endl;
        }
     }
     cout &lt;&lt; endl &lt;&lt; endl;
     int **secArray2D;
     allocate2DArrayPtr(&amp;secArray2D, &amp;sizeX, &amp;sizeY);
  
     for(int i=0; i&lt;sizeX; i++)
     {
        for(int j=0; j&lt;sizeY; j++)
        {
           cout &lt;&lt; "secArray2D[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "]:" &lt;&lt; secArray2D[i][j] &lt;&lt; endl;
        }
     }
  
     return 0;
  }
  
  void allocate2DArrayRef(int **&amp;locArray, int&amp; indexFirst, int&amp; indexSecond)
  {
      indexFirst = 4;
      indexSecond = 5;
      locArray = new int*[indexFirst];
      for(int i=0; i&lt;indexFirst ; i++)
      { 
          locArray[i] = new int[indexSecond];
          for(int j=0; j&lt;indexSecond; j++)
          {
              locArray[i][j] = i*j;
          }
      }
  }
  
  void allocate2DArrayPtr(int ***locArray, int *indexFirst, int *indexSecond)
  {
     *indexFirst = 2;
     *indexSecond = 3;
      int **temp = *locArray;
      temp = new int*[*indexFirst];
      for(int i=0; i&lt;(*indexFirst) ; i++)
      {
         temp[i] = new int[*indexSecond];
         for(int j=0; j&lt;(*indexSecond); j++)
         {
             temp[i][j] = i+j;
         }
      }
  }
  </code></pre>