offsetof宏解析
来源:互联网 发布:修睿乔杉潘斌龙网络剧 编辑:程序博客网 时间:2024/05/18 21:49
offsetof宏解析
今天看代码时,发现一个有用的东东,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相对于首地址的偏移值。
- offsetof宏解析
- offsetof宏解析
- linux内核 宏offsetof 解析
- offsetof解析
- 结构体之offsetof宏详细解析
- 结构体之offsetof宏详细解析
- offsetof宏
- offsetof宏
- offsetof 函数解析
- offsetof(s,m)解析
- linux 内核中container_of和offsetof宏定义解析
- offsetof宏学习反思
- offsetof宏的使用
- offsetof 宏的简介
- offsetof宏的用法
- offsetof与container_of宏
- 宏offsetof(TYPE, MEMBER)
- 3.1.11 offsetof宏
- Dreamweaver cs6 中设置默认文档格式为html5的方法
- 正则表达式regular expression详述
- IllegalStateException: Can not perform this action after onSaveInstanceState - How to prevent?
- DOM4J
- 好东西是要分享的,下了一个优秀的USB PC分析软件,不逊于Bus Hound
- offsetof宏解析
- Errors were encountered while processing: rabbitmq-server
- simpleAdapter
- Eclipse中的快捷键功能
- 敏捷开发测试
- adb server is out of date. kill.....解决办法
- VNC Server in Virtual Mode does not start correctly on Ubuntu 12.04
- 我的儒家修身
- 使用jquery dataTable