# Memory management in conversions of variable length data types

**URL:** <https://forum.hdfgroup.org/t/memory-management-in-conversions-of-variable-length-data-types/9250>\
**Category:** C++ Users' Group\
**Created:** [December 13, 2021, 7:56pm UTC](https://forum.hdfgroup.org/t/memory-management-in-conversions-of-variable-length-data-types/9250 "2021-12-13T19:56:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon.burr.gh](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@jon.burr.gh](https://forum.hdfgroup.org/u/jon.burr.gh)\
**Post date:** [December 13, 2021, 7:56pm UTC](https://forum.hdfgroup.org/t/memory-management-in-conversions-of-variable-length-data-types/9250/1 "2021-12-13T19:56:47Z")

</div>

Hi

I had a question on the memory management when converting variable-length datatypes.  
Taking the following code as an example

```
#include "H5Cpp.h"
#include <vector>

int main()
{
    std::vector<int> data{2, 3, -4, 122};
    hvl_t original{data.size(), data.data()};
    H5::VarLenType sourceType(&H5::PredType::NATIVE_INT);
    H5::VarLenType targetType(&H5::PredType::NATIVE_DOUBLE);
    // Use this to hold the converted value
    hvl_t converted = original;
    sourceType.convert(targetType, 1, &converted, nullptr);
}

```

If I look at the memory that `converted.p` points to I can see that it does contain the correct bytes to represent the same numbers as doubles (including being larger).

My question is - am I responsible for freeing the memory that `converted.p` points to?  
I couldn’t find this information in the documentation… I don’t run into any problems if I call `std::free(converted.p)` (assuming I don’t then try and access the memory) but I don’t know if that’s a solid guarantee that I’m responsible for that memory.

Cheers,  
Jon

---

<div class="post-metadata">

**Author:** ![steven](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/steven/32/117_2.png) [@steven](https://forum.hdfgroup.org/u/steven)\
**Post date:** [December 13, 2021, 8:33pm UTC](https://forum.hdfgroup.org/t/memory-management-in-conversions-of-variable-length-data-types/9250/2 "2021-12-13T20:33:54Z")

</div>

Hi Jon,  
are you familiar with [valgrind](https://valgrind.org/)? One way to convince yourself is to set up a rig, then check if there is any bytes left on the heap/stack upon exit.

best: steve

---

<div class="post-metadata">

**Author:** ![gheber](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/gheber/32/1919_2.png) [@gheber](https://forum.hdfgroup.org/u/gheber)\
**Post date:** [December 13, 2021, 9:37pm UTC](https://forum.hdfgroup.org/t/memory-management-in-conversions-of-variable-length-data-types/9250/3 "2021-12-13T21:37:06Z")

</div>

Jon, you own it. The C-API for cleaning up is `H5Dvlen_reclaim()` pre-HDF5 1.12 and `H5Treclaim()` in HDF5 1.12+.

Best, G.

---

<div class="post-metadata">

**Author:** ![jon.burr.gh](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/j/ec9cab/32.png) [@jon.burr.gh](https://forum.hdfgroup.org/u/jon.burr.gh)\
**Post date:** [December 14, 2021, 2:17pm UTC](https://forum.hdfgroup.org/t/memory-management-in-conversions-of-variable-length-data-types/9250/4 "2021-12-14T14:17:48Z")

</div>

Perfect - thanks @gheber for the confirmation!  
@steven, I’m having to refamiliarise myself with valgrind right now anyway so this is a good recommendation for the future…
