I've been looking at some AOSP code, and I came across this (from a 5.0+ branch, under art/runtime/base/macros.h)
#define OFFSETOF_MEMBER(t, f) \ (reinterpret_cast<const char*>(&reinterpret_cast<t*>(16)->f) - reinterpret_cast<const char*>(16)) // NOLINT
This is used in calls such as:
MemberOffset(OFFSETOF_MEMBER(Class, dex_cache ));
Where Class is a class and dex_cache is a member under that class. This returns the offset of the field and then is used as the constructor of a MemberOffset class.
My question is why on earth would you want to hardcode that "16" in there? Wouldn't it make more sense just take the address of the member and subtract the base address of the class?Answer1:
You can't subtract base address of class. You could do that for base address of object, but you do not have any. So, the macro converts address 16 into that base address.
Why not 0? Lint would complain. Why not 1? Structure alignment problems.