offsetof

Looking for something similar to offsetof() for non-POD types

Hi, I'm looking for a way to obtain offsets of data members of a C++ class which is of non-POD nature. Here's why: I'd like to store data in HDF5 format, which seems most suited for my kind of material (numerical simulation output), but it is perhaps a rather C-oriented library. I want to use it through the C++ interface, which would ...

Why can't you use offsetof on non-POD strucutures in C++?

I was researching how to get the memory offset of a member to a class in C++ and came across this on wikipedia: In C++ code, you can not use offsetof to access members of structures or classes that are not Plain Old Data Structures. I tried it out and it seems to work fine. class Foo { private: int z; int func() {cout << ...

Why subtract null pointer in offsetof()?

Linux's stddef.h defines offsetof() as: #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) whereas the Wikipedia article on offsetof() (http://en.wikipedia.org/wiki/Offsetof) defines it as: #define offsetof(st, m) \ ((size_t) ( (char *)&((st *)(0))->m - (char *)0 )) Why subtract (char *)0 in the Wikipedia version? I...

member alignment in c struct-embedded union

Hi I am modifying a bit of C code, that goes roughly like this: typedef struct STRUCT_FOO { ULONG FooInfo; union { ULONG LongData; USHORT ShortData; UCHAR CharData; }; } FOO; ... FOO foo; ULONG dataLength = offsetof(FOO, CharData) + sizeof(foo.CharData); Obviously, the code tries to figure o...