Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180975
  • 博文数量: 57
  • 博客积分: 2215
  • 博客等级: 大尉
  • 技术积分: 635
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-09 15:47
个人简介

非淡泊无以明志,非宁静无以致远

文章分类

全部博文(57)

文章存档

2013年(12)

2011年(15)

2010年(30)

我的朋友

分类: C/C++

2010-12-05 11:48:29

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.

Return Value

offsetof returns the offset in bytes of the specified member from the beginning of its parent data structure. It is undefined for bit fields.
Remarks

The offsetof macro returns the offset in bytes of memberName from the beginning of the structure specified by structName. You can specify types with the struct keyword.

Note

offsetof is not a function and cannot be described using a C prototype.

跟踪代码发现定义如下:

#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相对于首地址的偏移值。
阅读(576) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-07 10:10:04

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com