1、前言

  今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大。offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址来获取结构体的地址。两个宏设计的很巧妙,值得学习。linux内核中有着两个宏的定义,并在链表结构中得到应用。不得不提一下linux内核中的链表,设计的如此之妙,只需要两个指针就搞定了。后续认真研究一下这个链表结构。

2、offsetof宏

  使用offsetof宏需要包含stddef.h头文件,实例可以参考:http://www.cplusplus.com/reference/cstddef/offsetof/

offsetof宏的定义如下:

#define offsetof(type, member) (size_t)&(((type*)0)->member)

  巧妙之处在于将地址0强制转换为type类型的指针,从而定位到member在结构体中偏移位置。编译器认为0是一个有效的地址,从而认为0是type指针的起始地址。

3、container_of宏

  使用container_of宏需要包含linux/kernel.h头文件,container_of宏的定义如下所示:

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

container_of宏分为两部分,

第一部分:const typeof( ((type *)0)->member ) *__mptr = (ptr);

通过typeof定义一个member指针类型的指针变量__mptr,(即__mptr是指向member类型的指针),并将__mptr赋值为ptr。

第二部分: (type *)( (char *)__mptr - offsetof(type,member) ),通过offsetof宏计算出member在type中的偏移,然后用member的实际地址__mptr减去偏移,得到type的起始地址,即指向type类型的指针。

第一部分的目的是为了将统一转换为member类型指针。

4、测试程序

 #include <stdio.h>
#include <stdlib.h> #define NAME_STR_LEN 32 #define offsetof(type, member) (size_t)&(((type*)0)->member) #define container_of(ptr, type, member) ({ \
const typeof( ((type *))->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );}) typedef struct student_info
{
int id;
char name[NAME_STR_LEN];
int age;
}student_info; int main()
{
size_t off_set = ;
off_set = offsetof(student_info, id);
printf("id offset: %u\n",off_set);
off_set = offsetof(student_info, name);
printf("name offset: %u\n",off_set);
off_set = offsetof(student_info, age);
printf("age offset: %u\n",off_set);
student_info *stu = (student_info *)malloc(sizeof(student_info));
stu->age = ;
student_info *ptr = container_of(&(stu->age), student_info, age);
printf("age:%d\n", ptr->age);
printf("stu address:%p\n", stu);
printf("ptr address:%p\n", ptr);
return ;
}

测试结果:

5、参考网址

http://blog.csdn.net/thomas_nuaa/article/details/3542572

http://blog.chinaunix.net/uid-28489159-id-3549971.html

http://blog.csdn.net/yinkaizhong/article/details/4093795

offsetof与container_of宏[总结]的更多相关文章

  1. linux中offsetof与container_of宏定义

    linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->M ...

  2. (转)offsetof与container_of宏[总结]

    1.前言 今天在看代码时,遇到offsetof和container_of两个宏,觉得很有意思,功能很强大.offsetof是用来判断结构体中成员的偏移位置,container_of宏用来根据成员的地址 ...

  3. offsetof与container_of宏分析

    offsetof宏:结构体成员相对结构体的偏移位置 container_of:根据结构体成员的地址来获取结构体的地址 offsetof 宏 原型: #define offsetof(TYPE, MEM ...

  4. 对offsetof、 container_of宏和结构体的理解

    offsetof 宏 #include<stdio.h> #define offsetoff(type, member)      ((int)&((type*)0)->me ...

  5. container_of宏

    title: container_of宏 date: 2019/7/24 15:49:26 toc: true --- container_of宏 解析 在linux链表结构中有这样一个宏,通过成员变 ...

  6. typeof, offsetof, container_of宏

    container_of宏实现如下: #define container_of(ptr, type, member) ({ \ )->member ) *__mptr = (ptr); \ (t ...

  7. offsetof宏与container_of宏

    offsetof宏与container_of宏1.由结构体指针进而访问各元素的原理(1)通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这个时候其实是 ...

  8. typeof、offsetof、container_of的解释

    链表是内核最经典的数据结构之一,说到链表就不得不提及内核最经典(没有之一)的宏container_of. container_of似乎就是为链表而生的,它的主要作用是根据一个结构体变量中的一个域成员变 ...

  9. container_of宏定义分析---linux内核

    问题:如何通过结构中的某个变量获取结构本身的指针??? 关于container_of宏定义在[include/linux/kernel.h]中:/*_** container_of - cast a ...

随机推荐

  1. ios优秀的第三方框架

    1.数据请求,object-c  AFNetworking 网址:https://github.com/AFNetworking/AFNetworking swift   Alamofire 网址:h ...

  2. QtTest模块出现控制台的原因与方案

    转到Qt安装目录下的mkspces/features目录下, 1.用记事本打开qtestlib.prf文件,注释掉CONFIG += console #CONFIG += console 2.用记事本 ...

  3. CRC32 of Ether FCS with STM32

    Everyone knows that STM32F1xx, STM32F2xx, STM32F4xx have a hardware unit with a polynomial CRC32 0x0 ...

  4. 修復 “Failed to bring up eth0″ in Ubuntu virtualbox

    参考文献: 修復 “Failed to bring up eth0″ in Ubuntu VMWare ubuntu下smokeping安装配置 背景 从同事那边拷贝了一个virtualbox虚拟机过 ...

  5. [Winform]只允许运行一个exe,如果已运行则将窗口置前

    摘要 接着介绍项目中用到的一些方法,在winform中,打好包,有时并不允许运行多个客户端,要保证只有一个客户端运行.如果已经运行了,则弹出已运行的窗口,使其展示. 方法 判断是否有相同的进程 /// ...

  6. JBPM使用方法、过程记录

    一.How to call Web Service Using JBPM 5, designer https://204.12.228.236/browse.php?u=ObFK10b3HDFCQUN ...

  7. lodash用法系列(3),使用函数

    Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...

  8. future封装了callable,thread封装future。

    三.使用Callable,Future返回结果 总结:future封装了callable,thread封装future.将callable的返回结果封装在future中,thread封装future, ...

  9. 如何修改Mac截屏保存路径

    MAC OS X系统默认的截图路径是桌面文件夹,默认的截图格式是 PNG 图片格式,如何自定义设置呢? 截图保存路径 打开终端(Terminal)并输入如下命令: defaults write com ...

  10. ArcEngine二次开发错误编码对照表(转)

    阅读数:3323 每当我们在进行AE开发,出现错误时经常会出现错误代码,但是我们并不知道它到底代表什么意思,这里的而错误编码我们可以对照着找到我们需要的时候常详细信息(问题是,经常还是会出现没有错误编 ...