字节对齐及求结构体成员变量偏移量
先上代码
#include<cstdio> #include<cstring> #define OFFSET(TYPE, MEMBER, OFF) \ TYPE temp; \ OFF = (unsigned long)(&(temp.MEMBER)) - (unsigned long)(&(temp)); struct Type_t { char a; int b; double c; }; int main() { int offset = 0; Type_t type; offset = (unsigned long)(&(type.c)) - (unsigned long)((&type)); printf("offset = %d\n", offset); int offset_1 = 0; OFFSET(Type_t, c, offset_1); printf("offset = %d\n", offset_1); return 0; }
offset和offset_1都是求偏移量的。
https://blog.csdn.net/encourage2011/article/details/52463857