# Variable length compound data?

**URL:** <https://forum.hdfgroup.org/t/variable-length-compound-data/1120>\
**Category:** Hdf-forum archives\
**Created:** [September 2, 2008, 2:44pm UTC](https://forum.hdfgroup.org/t/variable-length-compound-data/1120 "2008-09-02T14:44:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anal\_K\_Patel](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/a/b38774/32.png) [@Anal\_K\_Patel](https://forum.hdfgroup.org/u/Anal_K_Patel)\
**Post date:** [September 2, 2008, 2:44pm UTC](https://forum.hdfgroup.org/t/variable-length-compound-data/1120/1 "2008-09-02T14:44:33Z")

</div>

VERSION:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HDF5 release 1.8

DESCRIPTION OF QUESTION:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We have a nested structure to write data. The inner  
structure can be array of any length. It is not known ahead of time during  
the creation of datatype the length or dimension of this struct. As a  
matter of fact outer structure array of length is also not known. But that  
I believe could be managed by chunking to the size of one struct, and then  
extending it each time a new outer struct has to be written.  
for example:  
typedef struct sample1 {  
int val4;  
}sample1;  
typedef struct sample {  
int val1;  
char val2;  
sample1 s1[]; // How many records of s1 are there is not known upfront  
double val3;  
} sample;

sample spl[100];  
spl[1] can have 20 s1's; spl[2] can have 50 s1's, ...

[Note: Although the data gets originated from the struct example above,  
the module writing HDF has no knowledge of defined C/C++ struct which may  
reside into header file of some other program. However, the hdf module  
does get information like STRUCT\_SAMPLE1\_START, INT  
VAL4,\<data\>,STRUCT\_SAMPLE1\_END, etc.]

Is there anyway, we can have variable length of compound data type. There  
will be one dataset wherein 100 records of struct sample will be stored.  
The length of the sample may vary depending on number of s1 records it may  
contain. The max value of s1 as an array of structs is also not known.

Can we have one dataset with defined datatypes, but different sizes of  
each sample record? If there is other appropriate way of structuring and  
writing this data, I would appreciate your inputs.

Thanks,  
Anal Patel

---

<div class="post-metadata">

**Author:** ![Quincey\_Koziol](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/q/dbc845/32.png) [@Quincey\_Koziol](https://forum.hdfgroup.org/u/Quincey_Koziol)\
**Post date:** [September 4, 2008, 3:51am UTC](https://forum.hdfgroup.org/t/variable-length-compound-data/1120/2 "2008-09-04T03:51:28Z")

</div>

Hi Anal,

> VERSION:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HDF5 release 1.8
> 
> DESCRIPTION OF QUESTION:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We have a nested structure to write data. The inner structure can be array of any length. It is not known ahead of time during the creation of datatype the length or dimension of this struct. As a matter of fact outer structure array of length is also not known. But that I believe could be managed by chunking to the size of one struct, and then extending it each time a new outer struct has to be written.  
> for example:  
> typedef struct sample1 {  
> int val4;  
> }sample1;  
> typedef struct sample {  
> int val1;  
> char val2;  
> sample1 s1; // How many records of s1 are there is not known upfront  
> double val3;  
> } sample;
> 
> sample spl[100];  
> spl[1] can have 20 s1's; spl[2] can have 50 s1's, ...
> 
> [Note: Although the data gets originated from the struct example above, the module writing HDF has no knowledge of defined C/C++ struct which may reside into header file of some other program. However, the hdf module does get information like STRUCT\_SAMPLE1\_START, INT VAL4,\<data\>,STRUCT\_SAMPLE1\_END, etc.]
> 
> Is there anyway, we can have variable length of compound data type. There will be one dataset wherein 100 records of struct sample will be stored. The length of the sample may vary depending on number of s1 records it may contain. The max value of s1 as an array of structs is also not known.
> 
> Can we have one dataset with defined datatypes, but different sizes of each sample record? If there is other appropriate way of structuring and writing this data, I would appreciate your inputs.

&nbsp;&nbsp;Yes, this is supported in HDF5. You can define a datatype that's a variable-length sequence of a compound datatype (like sample1 above) and then use that VL sequence datatype for a field in another compound datatype. The code in the test/vltypes.c source file in the HDF5 distribution works through lots of combinations of this sort. Perhaps Elena has a standalone example that she could point to also...

&nbsp;&nbsp;Quincey

> **···**
>
> On Sep 2, 2008, at 9:44 AM, Anal K Patel wrote:
> 
> ----------------------------------------------------------------------  
> This mailing list is for HDF software users discussion.  
> To subscribe to this list, send a message to hdf-forum-subscribe@hdfgroup.org.  
> To unsubscribe, send a message to hdf-forum-unsubscribe@hdfgroup.org.
