有这个宏offsetof在C/C++,让您获得地址的POD结构成员的偏移.有关C FAQ的示例:
struct foo {
int a;
int b;
};
struct foo;
/* Set the b member of foo indirectly */
*(int *)((char *)foo + offsetof(b)) = 0xDEADBEEF;
现在这对我来说似乎是邪恶的,我看不出这个宏的许多合法用途.
我看到的一个合法的例子是它在Linux内核中的container_of宏中用于获取嵌入式结构父对象的地址:
/* get the address of the cmos device struct in which the cdev
   structure which inode points to is embedded */
struct cmos_dev *cmos_devp = 
     container_of(inode->i_cdev, struct cmos_dev, cdev);
这个宏有什么其他合法用途?你什么时候应该不会使用这个宏?
结构的属性是否在C++中继承
例如:
struct A {
    int a;
    int b;
}__attribute__((__packed__));
struct B : A {
    list<int> l;
};
struct B(struct A)的继承部分是否会继承packed属性?
我不能在没有得到编译器警告的情况下向struct B 添加一个属性((packed)):
ignoring packed attribute because of unpacked non-POD field
所以我知道整个结构B不会打包,这在我的用例中很好,但我要求struct A的字段打包在struct B中.