/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright by The HDF Group. * * Copyright by the Board of Trustees of the University of Illinois. * * All rights reserved. * * * * This file is part of HDF5. The full HDF5 copyright notice, including * * terms governing use, modification, and redistribution, is contained in * * the files COPYING and Copyright.html. COPYING can be found at the root * * of the source code distribution tree; Copyright.html can be found at the * * root level of an installed copy of the electronic HDF5 document set and * * is linked from the top-level documents page. It can also be found at * * http://hdfgroup.org/HDF5/doc/Copyright.html. If you do not have * * access to either file, you may request a copy from help@hdfgroup.org. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #ifdef OLD_HEADER_FILENAME #include #else #include #endif #include #ifndef H5_NO_NAMESPACE #ifndef H5_NO_STD using std::cout; using std::endl; #endif // H5_NO_STD #endif #include "H5Cpp.h" #ifndef H5_NO_NAMESPACE using namespace H5; #endif template T **AllocateDynamicArray(int nRows, int nCols) { T **dynamicArray; dynamicArray = new T*[nRows]; for( int i = 0 ; i < nRows ; i++ ) dynamicArray[i] = new T [nCols]; return dynamicArray; } #define SIZEROW 4 #define SIZECOL 3 void initResultsHDF(int **Results) { H5File* file = new H5File( "_myfile.h5", H5F_ACC_TRUNC ); hsize_t dims[] = {SIZEROW, SIZECOL}; DataSpace fspace( 2, dims ); DataSet dataset(file->createDataSet("mydset", PredType::NATIVE_INT, fspace)); // define a memory space to write each row hsize_t row[] = {SIZECOL}; DataSpace mspace( 1, row ); //Then I fill the array with all 0’s and try to print it but this is the line that gives the error. for( int i = 0 ; i < SIZEROW ; i++ ) { // select file hyperslab hsize_t start[2]; // start of hyperslab hsize_t stride[2] = {1, 1}; // stride of hyperslab hsize_t count[2]; // block count hsize_t block[2] = {1, 1}; // block sizes count[0] = 1; count[1] = SIZECOL; start[0] = i; start[1] = 0; fspace.selectHyperslab( H5S_SELECT_SET, count, start, stride, block); // select memory hyperslab hsize_t mstart[1] = {0}; // start of hyperslab hsize_t mstride[1] = {1}; // stride of hyperslab hsize_t mcount[1] = {SIZECOL}; // block count hsize_t mblock[1] = {1}; // block sizes mspace.selectHyperslab( H5S_SELECT_SET, mcount, mstart, mstride, mblock); int *row_start = Results[i]; printf("writing row %d\n", i ); for( int j = 0 ; j < SIZECOL ; j++ ) printf("%d ", Results[i][j]); printf("\n"); dataset.write(row_start, PredType::NATIVE_INT, mspace, fspace); } delete file; } int main() { int **Results = AllocateDynamicArray(SIZEROW,SIZECOL); //Correctly creates a dynamic multidimensional array int n = 1; for( int i = 0 ; i < SIZEROW ; i++ ) { for( int j = 0 ; j < SIZECOL ; j++ ) { Results[i][j] = n++; } } initResultsHDF(Results); //Sends the dynamically created multidimensional array to the function return 0; }