container_of 和 offsetof宏
2018-03-01 11:46
453 查看
在linux 驱动源码中,有两个很有意思的宏,分别是offsetof和container_of,他们的作用和定义是这样子的
offsetof 获得结构体某成员相对于结构体的偏移长度
这个实现很巧妙,相当于设结构体的整体地址设成0,然后它的成员的地址就相当于成员的偏移
测试代码:
执行结果:
offset ca is 0
offset ib is 4
offset dc is 8
2.container_of 是通过结构体的某成员的地址得到整个结构体的地址,在使用上进而可以访问到结构体的其余成员
实现上实际就是当前这个成员的地址 减去 这个成员相对整个结构体的的偏移量,就是结构体的地址
测试代码
执行结果
name :zhangsan
phone_num :15617274954
index :110
\============
name :zhangsan
phone_num :15617274954
index :110
offsetof 获得结构体某成员相对于结构体的偏移长度
/** * 计算的结构体中某成员的偏移量 */ #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
这个实现很巧妙,相当于设结构体的整体地址设成0,然后它的成员的地址就相当于成员的偏移
测试代码:
#include <stdio.h> #include "list.h" typedef struct Test_struct { char ca; int ib; double dc; }Test_struct_t; int main() { printf("offset ca is %d\n",offsetof(Test_struct_t,ca)); printf("offset ib is %d\n",offsetof(Test_struct_t,ib)); printf("offset dc is %d\n",offsetof(Test_struct_t,dc)); return 0; }
执行结果:
offset ca is 0
offset ib is 4
offset dc is 8
2.container_of 是通过结构体的某成员的地址得到整个结构体的地址,在使用上进而可以访问到结构体的其余成员
/** * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. * 通过结构体中的某个成员的指针,反推出来结构体的指针,可以进而可以访问其他成员 */ #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );})
实现上实际就是当前这个成员的地址 减去 这个成员相对整个结构体的的偏移量,就是结构体的地址
测试代码
typedef struct person { char name[20]; char phone_num[12]; int index; }Person_t; void showInfo(Person_t *ptr) { printf("name :%s\n",ptr->name); printf("phone_num :%s\n",ptr->phone_num); printf("index :%d\n",ptr->index); } // 通过姓名打印所以信息 void getInfoByName(char name[]) { if(!name) return ; Person_t *ptr = container_of(name,Person_t,name); if(ptr) { showInfo(ptr); } } int main() { Person_t person; memset(&person,0x0,sizeof(Person_t)); person.index = 110; strncpy(person.name,"zhangsan",strlen("zhangsan")); strncpy(person.phone_num,"15617274954",strlen("15617274954")); showInfo(&person); printf("\============\n"); getInfoByName(&person.name); return 0; }
执行结果
name :zhangsan
phone_num :15617274954
index :110
\============
name :zhangsan
phone_num :15617274954
index :110
相关文章推荐
- offsetof宏实现以及container_of《代码》
- container_of
- linux kernel typeof container_of
- 对container_of(ptr,type,member)分析
- linux内核之container_of(ptr, type, member)
- 内核:offsetof + container_of 分析
- container_of()
- eclipse 第三方jar包 关联源码,提示 The Jar of this class file belongs to container 'Android Dependencies'
- LINUX之container_of解析
- container_of详解
- The Magical container_of() Macro
- container_of(ptr, type, member)
- container_of()宏
- ‘weird’ kernel macros — container_of
- container_of的理解
- container_of函数和list_entry函数
- container_of详解
- 【已解决】Android的ADT调试期间出现提示:The Jar of this class file belongs to container ‘Android Dependencies’ whic
- container_of
- 由linux内核某个片段(container_of)引发的对于C语言的深入理解