c++ - can't understand a macro definition (casting constant number to a class pointer) -
in code, see macro defined below can't understand.
#define offset_of_field_(f) (reinterpret_cast<char*>( \ &reinterpret_cast<netparameter*>(16)->f) - \ reinterpret_cast<char*>(16))
the macro name seems calculating offset of field f inside class structure. , has form of subtracting start address address of field. how number 16 used here? , deosn't reinterpret_case apply 16?(not 16 -> f). appreciate if please explain code me.
the comment in (now refactored) protobuf header (link here) explains it
// note calculate relative pointer value 16 here since if // use zero, gcc complains dereferencing null pointer. // choose 16 rather other number in case compiler // confused unaligned pointer. #define google_protobuf_generated_message_field_offset(type, field) \ static_cast<int>( \ reinterpret_cast<const char*>( \ &reinterpret_cast<const type*>(16)->field) - \ reinterpret_cast<const char*>(16)) #endif
so reason why 16 being used twofold:
- avoid using null pointer
- use aligned pointer
be aware known create issues (might replaced __builtin_offsetof
supported).
Comments
Post a Comment