Hi all,
I am using compound data types with vlen strings and integers. The struct containing the strings and integers also contains a couple of methods, which prevents me from using the HOFFSET macro, so I used sizeof() and/or the getSize() method from the H5 data types to determine the offset of the components. This works under my personal 32-bit system, but ends with a segmentation fault under a 64-bit system. I found out that the offset determined by HOFFSET is different from the sizeof() and getSize() methods. I avoided this problem by inserting the int as the last element.
A small sample code is added at the end of this mail.

My test system is:
> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 9.10
Release:        9.10
Codename:       karmic
> uname -a
Linux xxxxx 2.6.31-22-generic #70-Ubuntu SMP Wed Dec 1 23:48:17 UTC 2010 x86_64 GNU/Linux
> gcc --version
gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1

Is this a general problem/issue or due wrong usage?

~Mathias

---

#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

#include "H5Cpp.h"

using namespace H5;

char* tc_string(const string& s) {
        char* ret = new char[s.length() + 1];
        size_t l = s.copy(ret, s.length(), 0);
        ret[l] = 0;
        return ret;
}

int main(int argc, char* argv[]) {

        {
                H5File* file = new H5File("test1.h5", H5F_ACC_TRUNC);

                StrType stringtype(PredType::C_S1, H5T_VARIABLE);

                struct cv {
                        char* id;
                        int accession;
                };
                cout << "char* first: " << endl;
                cout << "  sizeof(char*)          " << sizeof(char*) << endl;
                cout << "  stringtype.getSize()   " << stringtype.getSize() << 
endl;
                cout << "  HOFFSET(cv, accession) " << HOFFSET(cv, accession) 
<< endl;

                CompType cvtype(sizeof(cv));
                size_t offset = 0;
                cvtype.insertMember("id", offset, stringtype);
                offset += stringtype.getSize();
                cvtype.insertMember("accession", offset, PredType::NATIVE_INT);
                offset += PredType::NATIVE_INT.getSize();

                cv* test = new cv[2];
                test[0].accession = 12;
                test[0].id = tc_string("1.1");
                test[1].accession = 23;
                test[1].id = tc_string("2.2");

                DSetCreatPropList cparms;
                hsize_t dimds[1] = { 2 };
                DataSpace dataspace(1, dimds, dimds);
                DataSet ds = file->createDataSet("test1", cvtype, dataspace, 
cparms);
                ds.write(test, cvtype);
                for (int i = 0; i < 2; ++i) {
                        delete[] test[i].id;
                }
                delete[] test;
                delete file;
        }

        cout << endl;

        {
                H5File* file = new H5File("test2.h5", H5F_ACC_TRUNC);

                StrType stringtype(PredType::C_S1, H5T_VARIABLE);

                struct cv {
                        int accession;
                        char* id;
                };

                cout << "int* first:" << endl;
                cout << "  sizeof(int)          " << sizeof(int) << endl;
cout << " PredType::NATIVE_INT " << PredType::NATIVE_INT.getSize() << endl;
                cout << "  HOFFSET(cv, id)      " << HOFFSET(cv, id) << endl;

                CompType cvtype(sizeof(cv));
                size_t offset = 0;
                cvtype.insertMember("id", offset, stringtype);
                offset += stringtype.getSize();
                cvtype.insertMember("accession", offset, PredType::NATIVE_INT);

                cv* test = new cv[2];
                test[0].accession = 12;
                test[0].id = tc_string("1.1");
                test[1].accession = 23;
                test[1].id = tc_string("2.2");

                DSetCreatPropList cparms;
                hsize_t dimds[1] = { 2 };
                DataSpace dataspace(1, dimds, dimds);
                DataSet ds = file->createDataSet("test2", cvtype, dataspace, 
cparms);
                ds.write(test, cvtype);
                for (int i = 0; i < 2; ++i) {
                        delete[] test[i].id;
                }
                delete[] test;
                delete file;
        }
        return 0;
}

_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

Reply via email to