title: Reading hdf5 files to dynamic arrays in c++
body: <p>I am trying to read large 3D hdf5 files into a dynamic array due to size limitations on the stack.  I have tried several different methods and have failed via segmentation fault.  Below is example code to show my issue.  I would very much appreciate some help!!</p>
  
  <pre><code>//This example was based on several examples which came in the c++ examples directory of the hdf5 package.
  
  
  #ifdef OLD_HEADER_FILENAME
  #include &lt;iostream.h&gt;
  #else
  #include &lt;iostream&gt;
  #endif
  #include &lt;string&gt;
  #include &lt;new&gt;
  #include "hdf5.h"
  
  #include "H5Cpp.h"
  
  #ifndef H5_NO_NAMESPACE
      using namespace H5;
  #endif
  
  const H5std_string  FILE_NAME( "Test.h5" );
  const H5std_string  DATASET_NAME( "Identity" );
  const int   NX = 3;                    // dataset dimensions
  const int   NY = 3;
  const int   RANK = 2;
  
  using namespace std;
  
  int main (void)
  {
  
      int buffer[3][3];
  
  
  //  Make Identity Matrix for fill.
      for(int i=0; i&lt;NX; i++){
          for(int j=0; j&lt;NY; j++){
              if(i==j) buffer[i][j] = 1;
              else buffer[i][j]=0;
          }
      }
  cout &lt;&lt; "Lets check we filled it correctly: " &lt;&lt; endl;
  //  First let's check we do have I matrix
  
      for(int i=0; i&lt;NX; i++){
          for(int j=0;j&lt;NY;j++){
              cout &lt;&lt; buffer[i][j] &lt;&lt; "  ";
          } cout &lt;&lt; endl;
      }
  
  
  
  
  //  Now let's write into a file
      H5File file( FILE_NAME, H5F_ACC_TRUNC );
      hsize_t     dimsf[2];
      dimsf[0] = NX;
      dimsf[1] = NY;
      DataSpace dataspace( RANK, dimsf );
      IntType datatype( PredType::NATIVE_INT );
      datatype.setOrder( H5T_ORDER_LE );
      DataSet dataset = file.createDataSet( DATASET_NAME, datatype, dataspace );
      dataset.write( buffer, PredType::NATIVE_INT );
  
  
  
  //  Ok great, now let's try opening this array using both static and dynamic(new) type arrays:
  
      H5File file1( FILE_NAME, H5F_ACC_RDONLY );
      DataSet dataset1 = file1.openDataSet( DATASET_NAME );
  
      /*
       * Get filespace for rank and dimension
       */
      DataSpace filespace = dataset1.getSpace();
  
      /*
       * Get number of dimensions in the file dataspace
       */
      int rank = filespace.getSimpleExtentNdims();
  
      /*
       * Get and print the dimension sizes of the file dataspace
       */
      hsize_t dims[2];    // dataset dimensions
      rank = filespace.getSimpleExtentDims( dims );
      /*
       * Define the memory space to read dataset.
       */
      DataSpace mspace1(RANK, dims);
  
  
      int newbuffer[NX][NY]; //static array.
      int **p2DArray;
  
      p2DArray = new int*[NX];
      for (uint i = 0; i &lt; NX; ++i) {
          p2DArray[i] = new int[NY];
      }
  
      dataset1.read( newbuffer, PredType::NATIVE_INT, mspace1, filespace );
      dataset1.read( p2DArray, PredType::NATIVE_INT, mspace1, filespace );
  
  //  Lastly lets print the arrays to make sure we get the same thing:
      cout &lt;&lt; "Ok and now for the static array:" &lt;&lt; endl;
  
      for(uint i=0; i&lt;NX; i++){
          for(uint j=0;j&lt;NY;j++){
              cout &lt;&lt; newbuffer[i][j] &lt;&lt; "  ";
          } cout &lt;&lt; endl;
      }
      cout &lt;&lt; "Ok and now for the dynamic array:" &lt;&lt; endl;
  
      for(uint i=0; i&lt;NX; i++){
          for(uint j=0;j&lt;NY;j++){
              cout &lt;&lt; p2DArray[i][j] &lt;&lt; "  ";
          } cout &lt;&lt; endl;
      }
  
  return 0;
  }
  </code></pre>