problem in write 3D array - allocation issue

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);*
*} *

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.
Hdf-forum@hdfgroup.org
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

I try one simple example of H5 write. I only change the allocation method
to dynamic and the data is getting corrupted.

http://www.hdfgroup.org/ftp/HDF5/current/src/unpacked/examples/h5_write.c

···

On Sun, Sep 30, 2012 at 11:58 AM, <hdf-forum-request@hdfgroup.org> wrote:

Send Hdf-forum mailing list submissions to
        hdf-forum@hdfgroup.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
or, via email, send a message with subject or body 'help' to
        hdf-forum-request@hdfgroup.org

You can reach the person managing the list at
        hdf-forum-owner@hdfgroup.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Hdf-forum digest..."

Today's Topics:

   1. Re: Hdf-forum Digest, Vol 39, Issue 27 (Maicon Faria)

----------------------------------------------------------------------

Message: 1
Date: Sun, 30 Sep 2012 11:58:53 -0300
From: Maicon Faria <maiconsaulfaria@gmail.com>
To: hdf-forum@hdfgroup.org
Subject: Re: [Hdf-forum] Hdf-forum Digest, Vol 39, Issue 27
Message-ID:
        <
CAEbZ8RdGJ9bfXG3HH2EGLQKj93dFAgJV_mNOrP6BxNao-6jFzQ@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Thanks for your answer Calixto, but I need to get a 3D hdf file, this file
will be convert in 2D snapshot where the third dimension is the time usind
hdfutils.
Even so, I try to define a 1D array as you said but keep RANK=3 and three
elemente dimsf[3], it works for cubic 3D arrays. But for non equal sides
there some offset fail in every X,Y,Z combination;

Also, I think that I could work with n-dimensions matrices on HDF files.

On Sat, Sep 29, 2012 at 12:31 PM, <hdf-forum-request@hdfgroup.org> wrote:

> Send Hdf-forum mailing list submissions to
> hdf-forum@hdfgroup.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
> or, via email, send a message with subject or body 'help' to
> hdf-forum-request@hdfgroup.org
>
> You can reach the person managing the list at
> hdf-forum-owner@hdfgroup.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Hdf-forum digest..."
>
>
> Today's Topics:
>
> 1. problem in write 3D array - allocation issue (Maicon Faria)
> 2. Re: problem in write 3D array - allocation issue (Calixte Denizet)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 28 Sep 2012 16:59:59 -0300
> From: Maicon Faria <maiconsaulfaria@gmail.com>
> To: hdf-forum@hdfgroup.org
> Subject: [Hdf-forum] problem in write 3D array - allocation issue
> Message-ID:
> <CAEbZ8ReGSfuHfGWB9=
> 7SxcB-4Vx79tN+ertDiUyejecjPEcb2A@mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
>
> 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);*
> *} *
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
>
http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20120928/16d0d245/attachment-0001.html
> >
>
> ------------------------------
>
> Message: 2
> Date: Sat, 29 Sep 2012 17:30:12 +0200
> From: Calixte Denizet <calixte.denizet@scilab-enterprises.com>
> To: hdf-forum@hdfgroup.org
> Subject: Re: [Hdf-forum] problem in write 3D array - allocation issue
> Message-ID: <50671404.3020203@scilab-enterprises.com>
> Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
>
> 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.
> > Hdf-forum@hdfgroup.org
> > 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
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
>
http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20120929/cdee719f/attachment.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Hdf-forum is for HDF software users discussion.
> Hdf-forum@hdfgroup.org
> http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
>
>
> ------------------------------
>
> End of Hdf-forum Digest, Vol 39, Issue 27
> *****************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <
http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20120930/79652734/attachment.html
>

------------------------------

Subject: Digest Footer

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

------------------------------

End of Hdf-forum Digest, Vol 39, Issue 28
*****************************************

Thanks for your answer Calixto, but I need to get a 3D hdf file, this file
will be convert in 2D snapshot where the third dimension is the time usind
hdfutils.
Even so, I try to define a 1D array as you said but keep RANK=3 and three
elemente dimsf[3], it works for cubic 3D arrays. But for non equal sides
there some offset fail in every X,Y,Z combination;

Also, I think that I could work with n-dimensions matrices on HDF files.

I try one simple example of H5 write. I only change the allocation method
to dynamic and the data is getting corrupted.

http://www.hdfgroup.org/ftp/HDF5/current/src/unpacked/examples/h5_write.c

Thanks for your answer Calixto, but I need to get a 3D hdf file, this file
will be convert in 2D snapshot where the third dimension is the time usind
hdfutils.
Even so, I try to define a 1D array as you said but keep RANK=3 and three
elemente dimsf[3], it works for cubic 3D arrays. But for non equal sides
there some offset fail in every X,Y,Z combination;

Also, I think that I could work with n-dimensions matrices on HDF files.

···

On Sat, Sep 29, 2012 at 12:31 PM, <hdf-forum-request@hdfgroup.org> wrote:

Send Hdf-forum mailing list submissions to
        hdf-forum@hdfgroup.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org
or, via email, send a message with subject or body 'help' to
        hdf-forum-request@hdfgroup.org

You can reach the person managing the list at
        hdf-forum-owner@hdfgroup.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Hdf-forum digest..."

Today's Topics:

   1. problem in write 3D array - allocation issue (Maicon Faria)
   2. Re: problem in write 3D array - allocation issue (Calixte Denizet)

----------------------------------------------------------------------

Message: 1
Date: Fri, 28 Sep 2012 16:59:59 -0300
From: Maicon Faria <maiconsaulfaria@gmail.com>
To: hdf-forum@hdfgroup.org
Subject: [Hdf-forum] problem in write 3D array - allocation issue
Message-ID:
        <CAEbZ8ReGSfuHfGWB9=
7SxcB-4Vx79tN+ertDiUyejecjPEcb2A@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

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);*
*} *
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <
http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20120928/16d0d245/attachment-0001.html
>

------------------------------

Message: 2
Date: Sat, 29 Sep 2012 17:30:12 +0200
From: Calixte Denizet <calixte.denizet@scilab-enterprises.com>
To: hdf-forum@hdfgroup.org
Subject: Re: [Hdf-forum] problem in write 3D array - allocation issue
Message-ID: <50671404.3020203@scilab-enterprises.com>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

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.
> Hdf-forum@hdfgroup.org
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <
http://mail.hdfgroup.org/pipermail/hdf-forum_hdfgroup.org/attachments/20120929/cdee719f/attachment.html
>

------------------------------

Subject: Digest Footer

_______________________________________________
Hdf-forum is for HDF software users discussion.
Hdf-forum@hdfgroup.org
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

------------------------------

End of Hdf-forum Digest, Vol 39, Issue 27
*****************************************