Hi,
I’m in the process of integrating HDF5 infrastructure to store/retrieve our simulation results. There’s a need to store compound data type (POD structs) in some of the datasets. In particular, we need to store/retrieve std::string in those compound data types. The writing appears to be working just fine as I’m able to view it using hdf5view. Reading results in a crash because the string length (of the data member of POD struct) looks to be incorrectly set to a very large number.
GCC version: 6.3.0p2 with C++11 enabled.
OS: Linux Red Hat 7.7
Here’s a code snippet that illustrates the struct and performs read from the dataset:
struct layerLegend_hdf5 {
int layerId = 0;
std::string layerName;
}
// Register compound type
hid_t m_strType = H5Tcopy(H5T_C_S1);
H5Tset_size(m_strType, H5T_VARIABLE);
CompType compType = H5Tcreate(H5T_COMPOUND, sizeof(layerLegend_hdf5));
compType.insertMember(“layerId”, HOFFSET(layerLegend_hdf5, layerId), PredType::NATIVE_INT);
compType.insertMember(“layerName”, HOFFSET(layerLegend_hdf5, layerName), m_strType);
// Template function to read from a dataset:
template
Status readDataSet(const h5FilePtr &file, const std::string &dsPath,
const DataType &dataType, std::vector &values, bool clearValues=true) {
if (file && file->exists(dsPath)) {
auto dataset(file->openDataSet(dsPath));
// Get dataspace of the dataset.
DataSpace dataspace(dataset.getSpace());
// Get the number of dimensions in the dataspace.
const int rank(dataspace.getSimpleExtentNdims());
hsize_t dims_out[rank]; // Rank is always 1 in our case
dataspace.getSimpleExtentDims(dims_out, nullptr);
T* data_out(new T[dims_out[0]]);
if(clearValues) {
values.clear(); values.reserve(dims_out[0]);
}
if(data_out) {
dataset.read(data_out, dataType);
for (hsize_t i = 0; i < dims_out[0]; i++) {
values.emplace_back(data_out[i]);
}
delete [] data_out;
}
dataset.close();
return Status();
}
return Status::IOError("readDataSet, problem opening dataset: ", dsPath);
}
Like I mentioned above, the read appears to be Ok. But, when I retrieve the ‘layerName’ field of ‘layerLegend_hdf5’ struct, the expected string is there but the length of the string is set to a very large number. This causes other client code to crash.
Is there anything else that needs to be done when reading such compound data types that have variable length std::string data members? Please let me know.
Thanks
-Kat