# Question on Variable length datatypes -memory related

**URL:** <https://forum.hdfgroup.org/t/question-on-variable-length-datatypes-memory-related/1272>\
**Category:** Hdf-forum archives\
**Created:** [May 11, 2009, 9:34am UTC](https://forum.hdfgroup.org/t/question-on-variable-length-datatypes-memory-related/1272 "2009-05-11T09:34:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramakrishnan.iyer](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@ramakrishnan.iyer](https://forum.hdfgroup.org/u/ramakrishnan.iyer)\
**Post date:** [May 11, 2009, 9:34am UTC](https://forum.hdfgroup.org/t/question-on-variable-length-datatypes-memory-related/1272/1 "2009-05-11T09:34:33Z")

</div>

All,

Consider this -

Allocation of memory for a variable length data type ( which is intended  
to store dataset region references ) . I am assigning the length (  
non-zero and +ve) and allocating the memory using malloc .

offsets is of type hvl\_t and Data is the structure holding offsets .

Data.offsets.len=numPart;

Data.offsets.p =  
(hdset\_reg\_ref\_t\*)malloc(numPart\*sizeof(hdset\_reg\_ref\_t));

Deallocation of memory: I am freeing the memory as given below

if(Data.offsets.len\>0) free(Data.offsets.p);

I do not make a call to H5Dvlen\_reclaim .

Is there any chance of memory leak in this case ?

Regards

Ram

---

<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:** [May 12, 2009, 1:50am UTC](https://forum.hdfgroup.org/t/question-on-variable-length-datatypes-memory-related/1272/2 "2009-05-12T01:50:42Z")

</div>

Hi Ram,

> **···**
>
> On May 11, 2009, at 4:34 AM, Ramakrishnan Iyer wrote:
> 
> > All,
> > 
> > Consider this –
> > 
> > Allocation of memory for a variable length data type ( which is intended to store dataset region references ) . I am assigning the length ( non-zero and +ve) and allocating the memory using malloc .
> > 
> > offsets is of type hvl\_t and Data is the structure holding offsets .
> > 
> > Data.offsets.len=numPart;
> > 
> > Data.offsets.p = (hdset\_reg\_ref\_t\*)malloc(numPart\*sizeof(hdset\_reg\_ref\_t));
> > 
> > Deallocation of memory: I am freeing the memory as given below
> > 
> > if(Data.offsets.len\>0) free(Data.offsets.p);
> > 
> > I do not make a call to H5Dvlen\_reclaim .  
> > Is there any chance of memory leak in this case ?
> 
> &nbsp;&nbsp;Seems OK to me...
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Quincey

---

<div class="post-metadata">

**Author:** ![Vesa\_Paatero](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/v/76d3ee/32.png) [@Vesa\_Paatero](https://forum.hdfgroup.org/u/Vesa_Paatero)\
**Post date:** [May 12, 2009, 8:38am UTC](https://forum.hdfgroup.org/t/question-on-variable-length-datatypes-memory-related/1272/3 "2009-05-12T08:38:29Z")

</div>

Dear Ramakrishnan and other C programmers,  
The example you provided may be affected by the special case of numPart being 0. I think I read somewhere that malloc(0) is a valid operation which returns a unique pointer that should be freed as any malloc allocation. However, I'm not exactly sure if that is part of the prevalent version standard C or something less universal.  
I don't know if numPart==0 is a relevant possibility in your application, but my typical approach for avoiding memory allocation bugs is to initialize pointers (Data.offsets.p) to zero and free them with something like if(Data.offsets.p) free(Data.offsets.p);  
Best Regards,  
&nbsp;&nbsp;&nbsp;&nbsp;Vesa Paatero

> **···**
>
> From: Ramakrishnan Iyer [[mailto:ramakrishnan.iyer@altair.com](mailto:ramakrishnan.iyer@altair.com)]  
> Sent: 11. toukokuuta 2009 12:35  
> To: HDF forum  
> Subject: [hdf-forum] Question on Variable length datatypes -memory related  
> Importance: High
> 
> All,  
> Consider this -  
> Allocation of memory for a variable length data type ( which is intended to store dataset region references ) . I am assigning the length ( non-zero and +ve) and allocating the memory using malloc .  
> offsets is of type hvl\_t and Data is the structure holding offsets .  
> Data.offsets.len=numPart;  
> Data.offsets.p = (hdset\_reg\_ref\_t\*)malloc(numPart\*sizeof(hdset\_reg\_ref\_t));
> 
> Deallocation of memory: I am freeing the memory as given below  
> if(Data.offsets.len\>0) free(Data.offsets.p);  
> I do not make a call to H5Dvlen\_reclaim .  
> Is there any chance of memory leak in this case ?
> 
> Regards  
> Ram

---

<div class="post-metadata">

**Author:** ![ramakrishnan.iyer](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/r/3e96dc/32.png) [@ramakrishnan.iyer](https://forum.hdfgroup.org/u/ramakrishnan.iyer)\
**Post date:** [May 12, 2009, 8:45am UTC](https://forum.hdfgroup.org/t/question-on-variable-length-datatypes-memory-related/1272/4 "2009-05-12T08:45:01Z")

</div>

Dear Vesa,

On numPart being 0, the Data.offsets.len is made 0 and the memory is  
not allocated ( It's just that , this is not shown in the code below ) .

Regards

Ram

> **···**
>
> -----Original Message-----  
> From: Vesa Paatero [[mailto:Vesa.Paatero@biolinscientific.com](mailto:Vesa.Paatero@biolinscientific.com)]  
> Sent: Tuesday, May 12, 2009 2:08 PM  
> To: Ramakrishnan Iyer; HDF forum  
> Subject: RE: Question on Variable length datatypes -memory related
> 
> Dear Ramakrishnan and other C programmers,
> 
> The example you provided may be affected by the special case of numPart  
> being 0. I think I read somewhere that malloc(0) is a valid operation  
> which returns a unique pointer that should be freed as any malloc  
> allocation. However, I'm not exactly sure if that is part of the  
> prevalent version standard C or something less universal.
> 
> I don't know if numPart==0 is a relevant possibility in your  
> application, but my typical approach for avoiding memory allocation bugs  
> is to initialize pointers (Data.offsets.p) to zero and free them with  
> something like if(Data.offsets.p) free(Data.offsets.p);
> 
> Best Regards,
> 
> &nbsp;&nbsp;&nbsp;&nbsp;Vesa Paatero
> 
> From: Ramakrishnan Iyer [[mailto:ramakrishnan.iyer@altair.com](mailto:ramakrishnan.iyer@altair.com)]  
> Sent: 11. toukokuuta 2009 12:35  
> To: HDF forum  
> Subject: [hdf-forum] Question on Variable length datatypes -memory  
> related  
> Importance: High
> 
> All,
> 
> Consider this -
> 
> Allocation of memory for a variable length data type ( which is intended  
> to store dataset region references ) . I am assigning the length (  
> non-zero and +ve) and allocating the memory using malloc .
> 
> offsets is of type hvl\_t and Data is the structure holding offsets .
> 
> Data.offsets.len=numPart;
> 
> Data.offsets.p =  
> (hdset\_reg\_ref\_t\*)malloc(numPart\*sizeof(hdset\_reg\_ref\_t));
> 
> Deallocation of memory: I am freeing the memory as given below
> 
> if(Data.offsets.len\>0) free(Data.offsets.p);
> 
> I do not make a call to H5Dvlen\_reclaim .
> 
> Is there any chance of memory leak in this case ?
> 
> Regards
> 
> Ram
