offsetof(TYPE, MEMBER)宏和container_of(ptr, type, member)宏的实现
来源:互联网 发布:淘宝well运动怎么样 编辑:程序博客网 时间:2024/05/17 02:29
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
1. ( (TYPE *)0 ) 将零转型为TYPE类型指针;
2. ((TYPE *)0)->MEMBER 访问结构中的数据成员;
3. &( ( (TYPE *)0 )->MEMBER )取出数据成员的地址;
1. ( (TYPE *)0 ) 将零转型为TYPE类型指针;
2. ((TYPE *)0)->MEMBER 访问结构中的数据成员;
3. &( ( (TYPE *)0 )->MEMBER )取出数据成员的地址;
4.(size_t)(&(((TYPE*)0)->MEMBER))结果转换类型。巧妙之处在于将0转换成(TYPE*),结构以内存空间首地址0作为起始地址,则成员地址自然为偏移地址;
#define container_of(ptr, type, member) ({ \
const typeof(((type *)0)->member)*__mptr = (ptr); \
(type *)((char *)__mptr - offsetof(type, member)); })
1. 定义一个member 类型的指针__mptr,并把ptr指向的地址赋值给__mptr。
2. 取出member在type类型中的偏移位置。
3. 把member类型指针__mptr指向的地址减去member的偏移地址所得到的地址强制转换为type类型指针,则得到type的地址。
- offsetof(TYPE, MEMBER)宏和container_of(ptr, type, member)宏的实现
- offsetof(TYPE, MEMBER)和 container_of(ptr, type, member)
- container_of(ptr,type,member)宏函数
- container_of(ptr, type, member)宏定义解析
- container_of(ptr, type, member)宏定义
- container_of(ptr, type, member)
- container_of(ptr, type, member)
- container_of(ptr, type, member)
- container_of(ptr, type, member)
- container_of(ptr, type, member)
- container_of(ptr, type, member)
- container_of(ptr, type, member)
- 宏offsetof(TYPE, MEMBER)
- Container_of(ptr,type,member)宏的简单分析
- Linux内核中的常用宏--container_of(container_of(ptr, type, member)
- 浅析container_of(ptr, type, member)
- 浅析container_of(ptr, type, member)
- 浅析container_of(ptr, type, member)
- C++获取PE文件的入口点
- 各种内部排序方法的分析比较
- 《算法艺术与信息学竞赛》题目-提交方式对照表
- hibernate 怎么设置java实体类和数据库表名不一致的问题,和属性名不一样这么办
- C++指针
- offsetof(TYPE, MEMBER)宏和container_of(ptr, type, member)宏的实现
- 第二周项目5:玩日期
- BAE百度云平台的mysql数据库的使用(Java)
- 如何第一次寫Android Launcher Switcher就上手
- MifareClassic卡自定义keyA和keyB
- 常用Linux命令
- 用SecureCRT来上传和下载数据
- winscp使用SSH连接linux系统
- 激活指定窗口为当前窗口