2013-06-14 1 views
3

스택의 크기 제한 때문에 큰 3D hdf5 파일을 동적 배열로 읽으려고합니다. 몇 가지 다른 방법을 시도하고 세분화 오류를 통해 실패했습니다. 아래는 내 문제를 보여주는 예제 코드입니다. 나는 약간의 도움에 매우 감사 할 것이다!!동적 배열 hdf5 파일을 C++로 읽기

//This example was based on several examples which came in the c++ examples directory of the hdf5 package. 


#ifdef OLD_HEADER_FILENAME 
#include <iostream.h> 
#else 
#include <iostream> 
#endif 
#include <string> 
#include <new> 
#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<NX; i++){ 
     for(int j=0; j<NY; j++){ 
      if(i==j) buffer[i][j] = 1; 
      else buffer[i][j]=0; 
     } 
    } 
cout << "Lets check we filled it correctly: " << endl; 
// First let's check we do have I matrix 

    for(int i=0; i<NX; i++){ 
     for(int j=0;j<NY;j++){ 
      cout << buffer[i][j] << " "; 
     } cout << 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 < 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 << "Ok and now for the static array:" << endl; 

    for(uint i=0; i<NX; i++){ 
     for(uint j=0;j<NY;j++){ 
      cout << newbuffer[i][j] << " "; 
     } cout << endl; 
    } 
    cout << "Ok and now for the dynamic array:" << endl; 

    for(uint i=0; i<NX; i++){ 
     for(uint j=0;j<NY;j++){ 
      cout << p2DArray[i][j] << " "; 
     } cout << endl; 
    } 

return 0; 
} 

답변

5

귀하의 p2DArray이 int의 일반 (1D) 배열이 아니라 배열의 배열이어야합니다. 그것이 바로 read 메서드가 예상하는 것입니다.

int *p2DArray = new int[NX*NY]; 

및 다음 식을 사용하여 어레이 액세스 :

다만 NX * NY 번호를 저장하기에 충분한 공간을 할당 p2DArray을 [I] [J] = p2DArray [내가 NY의 +의 J를 *].

+0

감사합니다. 훌륭하게 작동합니다. – StarStrides