免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4138 | 回复: 7
打印 上一主题 下一主题

[数据结构] 宏container_of用法问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-09-04 15:39 |只看该作者 |倒序浏览
  1. #include <stdio.h>

  2. #define FIND(TYPE,MEMBER) (size_t)&(((TYPE*)0)->MEMBER)

  3. #define container_of(ptr, type, member) ({                      \
  4.         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  5.         (type *)( (char *)__mptr - offsetof(type,member) );})

  6. struct list_head {
  7.         struct list_head *next, *prev;
  8. };

  9. struct student
  10. {
  11.         int a;
  12.         char b[19];
  13.         double c;
  14.         struct list_head list;
  15. };

  16. int main(void)
  17. {       
  18.         struct student s= {1,{0},0.0,{NULL,NULL}};
  19.         struct list_head * ptr = &s.list;

  20.         printf("%08x\n",s);
  21.         printf("%08x\n",&s);
  22.         printf("%08x\n",&s.a);
  23.         printf("%08x\n",&s.b);
  24.         printf("%08x\n",&s.c);
  25.         printf("%08x\n",FIND(struct student,c));
  26.         struct student *stu  = container_of(ptr,struct student,s.list);//这一句上有问题。
  27.         printf("%08x\n",stu);
  28.         return 0;
  29. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-09-04 16:06 |只看该作者
  1. #include <stdio.h>

  2. #define FIND(TYPE,MEMBER) (size_t)&(((TYPE*)0)->MEMBER)

  3. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

  4. #define container_of(ptr, type, member) ({                      \
  5.         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  6.         (type *)( (char *)__mptr - offsetof(type,member) );})

  7. struct list_head {
  8.     struct list_head *next, *prev;
  9. };

  10. struct student
  11. {
  12.     int a;
  13.     char b[19];
  14.     double c;
  15.     struct list_head list;
  16. };

  17. int main(void)
  18. {        
  19.     struct student s= {1,{0},0.0,{NULL,NULL}};
  20.     struct list_head * ptr = &s.list;

  21.     printf("%08x\n",s);
  22.     printf("%08x\n",&s);
  23.     printf("%08x\n",&s.a);
  24.     printf("%08x\n",&s.b);
  25.     printf("%08x\n",&s.c);
  26.     printf("%08x\n",FIND(struct student,c));
  27.     struct student *stu  = container_of(ptr,struct student, list);//这一句上有问题。
  28.     printf("%08x\n",stu->a);
  29.     return 0;
  30. }
复制代码
你忘记声明 container_of 中的 offsetof 宏了.

而且在使用 container_of 的时候, 也有错误. 第三个参数, 是对象在结构体中的名称.

论坛徽章:
0
3 [报告]
发表于 2012-09-04 16:18 |只看该作者
你试了没呀,我在vc6上试还是编译不通回复 2# hk2305621


   

论坛徽章:
0
4 [报告]
发表于 2012-09-04 16:31 |只看该作者
我在 linux 下用 gcc 编译的.  是 ok 的.

VC 6 好像有自己的 offset 函数.
  1. $ gcc container.c
  2. container.c: In function ‘main’:
  3. container.c:28: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘struct student’
  4. container.c:29: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘struct student *’
  5. container.c:30: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘int *’
  6. container.c:31: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘char (*)[19]’
  7. container.c:32: warning: format ‘%08x’ expects type ‘unsigned int’, but argument 2 has type ‘double *’
  8. $ ./a.out
  9. 00000001
  10. bfc2b6e4
  11. bfc2b6e4
  12. bfc2b6e8
  13. bfc2b6fc
  14. 00000018
  15. 00000001
复制代码

论坛徽章:
0
5 [报告]
发表于 2012-09-04 16:49 |只看该作者
非常感谢!!!终于等到了。呵呵,能加个qq吗?我qq:718268255.回复 4# hk2305621


   

论坛徽章:
0
6 [报告]
发表于 2012-09-04 17:25 |只看该作者
回头找你. 在公司, 不能上Q. 我的Q. 394831530.

论坛徽章:
0
7 [报告]
发表于 2012-09-05 15:32 |只看该作者
楼主高手,把Linux程序移植到windows

论坛徽章:
0
8 [报告]
发表于 2012-09-06 13:58 |只看该作者
楼主问题解决了咋不来公布下结果呢?
linux的链表确实是个不错的设计,把它移植到windows应该很多人都干过吧,我以前也做过。
VC6确实有自己的offsetof,我在VC下的container_of是这么写的:
  1. #define container_of(ptr, type, member)                \
  2.         (type *)((char *)ptr - offsetof(type, member))
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP