In C:
offsetof usually a macro, and thanks to its definition, it is probably optimized by the compiler, so it comes down to a constant value. And even if it becomes an expression, it is small enough so that it does not cause almost any overhead.
For example, in the stddef.h file, it is defined as:
#define offsetof(st, m) ((size_t)(&((st *)0)->m))
In C ++:
Things get a little more complicated, as it needs to allow bias for members as methods and other variables. Thus, offsetof is defined as a macro to call another method:
#define offsetof(st, m) __builtin_offsetof(st, m)
If you only need this for structures, you are good enough with offsetof . Otherwise, I do not think it is possible.
source share