[C++] offsetof用法 - 计算成员地址的内存偏移量
转自:http://www.cppblog.com/lovedday/archive/2007/09/24/32801.html
今天看代码时,发现一个有用的东东,offsetof(s,m),这是一个宏,MSDN文档的说明如下:
Retrieves the offset of a member from the beginning of its parent structure.
size_t offsetof(
structName,
memberName?
);
Parameters
structName?
Name of the parent data structure.
memberName?
Name of the member in the parent data structure for which to determine the offset.
跟踪代码发现定义如下:
#define offsetof(s,m) (size_t)&(((s *)0)->m)
然后到网上查了一下,发现还真的是很有用,附带一位大侠的解说:
struct AAA?
{?
??? int i;?
??? int j;?
};?
struct AAA *pAAA;?
pAAA=new AAA;
这时,pAAA实际上是一个Pointer, 指向某一确定的内存地址,比如0x1234;?
而 pAAA->i 整体是一个int型变量,其地址是&(pAAA->i) ,'&'为取址运算符;?
那么&(pAAA->i)一定等于0x1234,因为i是结构体AAA的第一个元素。?
而&(pAAA->j)一定是0x1234 + 0x4 = 0x1238; 因为sizeof(int) = 4;
这个做法的巧妙之处就是:它把“0”作为上例中的pAAA,那么 &(pAAA->j)就是j的offset啦。
解析结果是:?
(s *)0 ,将 0 强制转换为Pointer to "s"?
可以记 pS = (s *)0 ,pS是指向s的指针,它的值是0;?
那么pS->m就是m这个元素了,而&(pS->m)就是m的地址,而在本例中就是offset啦?
再把结果强制转换为size_t型的就OK 了,size_t其实也就是int啦!!
也就是说:
0 ---> (s *)0
原来的0是数值类型,现在是结构体指针类型,尽管类型变了,但其值还是不变,也就是说还是0,但这个值的意义变了,现在是地址,而不是数值。
&(((s *)0)->m)求出字段m的地址值,但由于首地址是0,所以&(((s *)0)->m)求出字段m相对于首地址的偏移值。