Question on Variable length datatypes -memory related

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

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 ?

  Seems OK to me...

    Quincey

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,
    Vesa Paatero

···

From: Ramakrishnan Iyer [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

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]
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,

    Vesa Paatero

From: Ramakrishnan Iyer [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