Hi,
you must allocate an int array with size NX*NY*NZ and to access the
element i,j,k, just use:
data[i + NX*j + NX*NY*k].
Best regards
Calixte
On 28/09/2012 21:59, Maicon Faria wrote:
I'm with a problem when I try to write 3D int array using HDF.
The problem happens when I use malloc to dynamically allocate the 3D
int array, the hdf5 file shows wrong numbers.
When I allocate 3D int arrays with: /int data[NX][NY][NZ]/ all goes fine.
I try to print on screen all data with printf and I got correct answers.
I would appreciate some advices
Here is the source code:
//* /
/ * This example writes data to the HDF5 file./
/ * Data conversion is performed during write operation. /
/ *//
//
/#include<hdf5.h>/
/#include <stdio.h>/
/#include <stdlib.h>/
/
/
/#define FILE "SDS16.h5"/
/#define DATASETNAME "IntArray" /
/#define NX 5 /* dataset dimensions *//
/#define NY 5/
/#define NZ 5/
/#define RANK 3/
/
/
/int*** allocate3D(int l,int m,int n);/
/void deallocate3D(int*** arr3D,int l,int m);/
/
/
/
/
/int main (void)/
/{/
/ hid_t file, dataset; /* file and dataset handles *//
/ hid_t datatype, dataspace; /* handles *//
/ hsize_t dimsf[3]; /* dataset dimensions *//
/ herr_t status; /
/ int i,j,k;/
/// int data[NX][NY][NZ];/
/ int*** data; /* data to write *//
//
/
/
/ //Data and output buffer initialization. /
/ data = allocate3D(NX,NY,NZ);/
/
/
/ /*/
/ * Create a new file using H5F_ACC_TRUNC access,/
/ * default file creation properties, and default file/
/ * access properties./
/ *//
/ file = H5Fcreate(FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);/
/ /*/
/ * Describe the size of the array and create the data space for
fixed/
/ * size dataset. /
/ *//
/ dimsf[0] = NX;/
/ dimsf[1] = NY;/
/ dimsf[2] = NZ;/
/ dataspace = H5Screate_simple(RANK, dimsf, NULL); /
//
/ //Define datatype for the data in the file. We will store little
endian INT numbers./
/ datatype = H5Tcopy(H5T_NATIVE_INT);/
/ status = H5Tset_order(datatype, H5T_ORDER_LE);/
/
/
/ /*Create a new dataset within the file using defined dataspace and/
/ * datatype and default dataset creation properties.*//
//
/ dataset = H5Dcreate(file, DATASETNAME, datatype, dataspace,
H5P_DEFAULT);/
/ //Write the data to the dataset using default transfer properties./
/
/
//
/ for (k = 0; k < NZ; k++) /
/ {/
/ for (j = 0; j < NY; j++) /
/{/
/for (i = 0; i < NX; i++)/
/ {/
/ data[i][j][k] = k;//campo[i*j*NX];/
/ }/
/}/
/ }/
//
//
/ status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, data);/
//
/ deallocate3D(data,NX,NY);/
//
//
/ //Close/release resources./
//
/ H5Sclose(dataspace);/
/ H5Tclose(datatype);/
/ H5Dclose(dataset);/
/ H5Fclose(file);/
//
/ return 0;/
/}/
/
/
/
/
///allocate a 3D array/
/int ***allocate3D(int l,int m,int n)/
/{/
/int ***arr3D;/
/int i,j,k;/
/
/
/arr3D = (int***)malloc(l * sizeof(int **));/
/
/
/for(i=0;i<l;i++)/
/{/
/ arr3D[i] = (int**)malloc(m * sizeof(int*));/
/ for(j=0;j<m;j++)/
/ {/
/ arr3D[i][j] = (int*)malloc(n*sizeof(int));/
/ }/
/}/
/
/
/return arr3D;/
/}/
/
/
///deallocate a 3D array/
/void deallocate3D(int*** arr3D,int l,int m)/
/{/
/ int i,j;/
/
/
/ for(i=0;i<l;i++)/
/ {/
/ for(j=0;j<m;j++)/
/ {/
/ free(arr3D[i][j]);/
/ }/
/ free(arr3D[i]);/
/ }/
/ free(arr3D);/
/} /
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
--
Calixte Denizet
Software Development Engineer
-----------------------------------------------------------
Scilab Enterprises
143bis rue Yves Le Coz - 78000 Versailles, France
http://www.scilab-enterprises.com
_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org