随笔 - 1419  文章 - 388  trackbacks - 0


改变世界的是这样一群人
他们寻找梦想中的乐园
当找不到时
他们亲手创造了它

------------------------



Time is Life

让我们一步步将梦想变成现实

不要迷失在技术的海洋里

任何未经实践检验的理论都是危险而不可靠的

一个人若精神上得不到自由而成为别人思想的奴隶,那么这种人是最可悲的,毋宁死而不做奴隶。

------------------------


吾生也有涯,而知也无涯。
以有涯随无涯,殆已;
已而为知者,殆而已矣!

常用链接

随笔分类(448)

文章分类(17)

3D游戏编程相关链接

搜索

  •  

最新评论


今天看代码时,发现一个有用的东东,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相对于首地址的偏移值。

posted on 2007-09-24 19:04 lovedday 阅读(218) 评论(0)  编辑 收藏 引用 所属分类: ▲ C Program

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]

相关链接:
网站导航: