Unable to write STL matrices into h5 files

I have created the very simple code snippet, compiled in Visual Studio 2022, to write a matrix object (STL vector of vectors containing double elements) into a directory. I’ve followed all the steps to ensure there are no errors/exceptions running the code in (e.g. linker errors, making sure the directory exists and contains no file with the same name, etc.). But for some strange reason, when I run the code below, no h5 file appears in the output directory, despite no errors or warnings! Is the code snippet working for other users?

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


int main()
{
    using namespace H5;

    // Create a compound data type for the inner vector
    H5::CompType innerVectorType(sizeof(double));
    innerVectorType.insertMember("inner", 0, H5::PredType::NATIVE_DOUBLE);

    // Create a compound data type for the outer vector
    H5::CompType outerVectorType(sizeof(innerVectorType));
    outerVectorType.insertMember("outer", 0, innerVectorType);

    std::string outputPath = "D://Scratch//Test.h5";
    H5File file1(outputPath, H5F_ACC_EXCL);

    std::vector<std::vector<double>> data = { {1.0, 2.0, 3.0},
                                           {4.0, 5.0, 6.0},
                                           {7.0, 8.0, 9.0} };
    hsize_t dims[2] = { data.size(), data[0].size() };
    H5::DataSpace dataspace(2, dims);
    
    H5::DataSet dataset = file1.createDataSet("my_dataset", outerVectorType, dataspace);

    // Write the data to the dataset
    dataset.write(data.data(), outerVectorType);

    return 0;
}

The HDF5 C library H5Dwrite() call is going to expect a single block of memory that represents an array of data in row-major order. You’ll need to convert your vector of vectors to an array like that before you pass it in.

Thanks. I have modified the code to convert the STL vector of vectors to a single 1D array, held in contiguous memory, but I still have the same problem.

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


int main()
{
    using namespace H5;

    // Create a compound data type for the inner vector
    H5::CompType innerVectorType(sizeof(double));
    innerVectorType.insertMember("inner", 0, H5::PredType::NATIVE_DOUBLE);

    // Create a compound data type for the outer vector
    H5::CompType outerVectorType(sizeof(innerVectorType));
    outerVectorType.insertMember("outer", 0, innerVectorType);

    std::string outputPath = "D://Scratch//Test.h5";
    H5File file1(outputPath, H5F_ACC_TRUNC);

    std::vector<std::vector<double>> data = { {1.0, 2.0, 3.0},
                                           {4.0, 5.0, 6.0},
                                           {7.0, 8.0, 9.0} };

    //allocate the array
    double* out_array = new double [data.size() * data[0].size()];

    // Convert data to a row-major array
    for (size_t i = 0; i < data.size(); ++i)
        for (size_t j = 0; j < data[0].size(); ++j)
            out_array[i * data[0].size() + j] = data[i][j];

    hsize_t dims[2] = { data.size(), data[0].size() };
    H5::DataSpace dataspace(2, dims);
    
    H5::DataSet dataset = file1.createDataSet("my_dataset", outerVectorType, dataspace);

    // Write the data to the dataset
    dataset.write(out_array, outerVectorType);

    //deallocate the array
    delete[] out_array;

    return 0;
}