offsetof与container_of宏分析

offsetof宏:结构体成员相对结构体的偏移位置
container_of:根据结构体成员的地址来获取结构体的地址

offsetof 宏

原型:

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

(TYPE *)0非常巧妙,告诉编译器有一个指向结构体 TYPE 的指针,其地址是0,然后取该指针的 MEMBER 地址 &((TYPE *)0)->MEMBER,因为基址是0,所以这时获取到的 MEMBER 的地址就是相当于在结构体 TYPE 中的偏移量了。
Example:

#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>

struct TYPE{
    int mem;
    int member;
};

int main()
{
    struct TYPE type;
    printf("&type = %p\n", &type);
    printf("&type.member = %p\n", &type.member);
    printf("&((struct type *)0)->member = %lu\n", ((size_t)&((struct TYPE *)0)->member) );
    printf("offsetof(struct TYPE member) = %zd\n", offsetof(struct TYPE, member));
    return 0;
}
/*
result:
&type = 0x7ffc1104a110
&type.member = 0x7ffc1104a114
&((struct type *)0)->member = 4
offsetof(struct TYPE member) = 4
*/

container_of 宏

原型:linux-4.18.5

/**
 * 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) ({                  void *__mptr = (void *)(ptr);                       BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&                !__same_type(*(ptr), void),                         "pointer type mismatch in container_of()");        ((type *)(__mptr - offsetof(type, member))); })

网上所见更多是底下这个版本:

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

第一部分:void *__mptr = (void *)(ptr);const typeof( ((type *)0)->member ) *__mptr = (ptr);
两个的差别在于 __mptr 的类型一个是 void * ,一个是 type *。
void * 较为容易理解,下面来看看 type *:
关于 typeof 关键字其作用是返回变量的类型,简单理解就是如下,详细可参见GCC typeof在kernel中的使用——C语言的“编译时多态”

int a;
typeof(a) b; //这等同于int b;
typeof(&a) c; //这等同于int* c;

因此const typeof( ((type *)0)->member ) *__mptr = (ptr);的作用就是通过 typeof 获取结构体成员 member 的类型,然后定义一个这个类型的指针变量 __mptr 并将其赋值为 ptr。
第二部分:(type *)( (char *)__mptr - offsetof(type,member) ),通过offsetof宏计算出 member 在 type 中的偏移,然后用 member 的实际地址__mptr减去偏移,得到 type 的起始地址。从上面关于offsetof宏的 Example 也可以验证这一点:
&type.member = 0x7ffc1104a114 - &((struct type *)0)->member = 4 = &type = 0x7ffc1104a110

原文地址:https://www.cnblogs.com/ZhaoxiCheung/p/9610721.html

时间: 2024-08-26 02:27:43

offsetof与container_of宏分析的相关文章

(六)linux内核中的offsetof与container_of宏

参考: offsetof与container_of宏[总结] #define offsetof(type, member) (size_t)&(((type*)0)->member) #define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );}) 原文地

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

offsetof 宏 #include<stdio.h> #define offsetoff(type, member)      ((int)&((type*)0)->member) /* ((type*)0)->member 释义:声明一个相应类型的结构体指针,该指针指向0地址处.再通过该指针访问各元素.我们只要获取一个指针就能访问其内部的元素吗,可以这么搞?其实我是想联系全局和非全局变量,通过上面这个代码也许你不明白我要表达的意思.请继续慢慢看,直到本文后面,结合本文

linux中offsetof与container_of宏定义

linux内核中offsetof与container_of的宏定义 #define offsetof(TYPE, MEMBER)    ((size_t) &((TYPE *)0)->MEMBER) /** * container_of - cast a member of a structure out to the containing structure * @ptr:        the pointer to the member. * @type:       the type

【转】container_of宏 分析

在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * 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 thi

C语言之offsetof宏和container_of宏

首先我们要明白一点通过结构体变量来访问结构体中的各个元素时,其本质上是 通过指针的方式来实现访问的,只不过是这个时候编译器帮自动帮我们计算了每个 元素与结构体起始地址之间的偏移量而已 一:offsetof宏: #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) 1:参数与返回值分析: (1)TYPE是结构体类型,MEMBER是结构体中一个元素的元素名 (2)这个宏返回的是member元素相对于整个结构体变量的首地址的偏移量,

container_of宏和offsetof宏的总结

1.offsetof 宏 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 将地址0强制转换为type类型的指针,从而得到结构体成员member相对于结构体起始地址的偏移量. 2.container_of 宏 #define container_of(ptr, type, member) ({ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type

offsetof宏与container_of宏

offsetof宏与container_of宏1.由结构体指针进而访问各元素的原理(1)通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这个时候其实是编译器帮我们自动计算了偏移量).2.offsetof宏: #define offsetof(TYPE, MEMBER) ((int)&( (TYPE *)0)->MEMBER )(1)offsetof宏的作用是:用宏来计算结构体中某个元素和结构体首地址的偏移量(其实质是通过编译器来帮助我们计算)

C语言笔记(结构体与offsetof、container_of之前的关系)

关于结构体学习,需要了解:结构体的定义和使用.内存对齐.结构体指针.得到结构体元素的偏移量(offsetof宏实现) 一.复习结构体的基本定义和使用 1 typedef struct mystruct 2 { 3 int a; 4 char b; 5 double c; 6 }MyS1; 7 8 /* 9 函数功能:演示结构体的定义和使用 10 */ 11 void func1(void) 12 { 13 //定义时赋值 14 MyS1 s1 = { 15 .a =1, 16 .b =2, 17

C语言之offset_of宏和container_of宏

通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这时候其实是编译器帮我们自动计算了偏移量). 1:offset_of宏 作用:计算结构体中某个元素和结构体首地址的偏移量(其实质是通过编译器来帮我们计算). 定义: #define offsetof(TYPE, MEMBER) ((int) &((TYPE *)0)->MEMBER) 参数分析:TYPE是结构体类型,MEMBER是结构体中一个元素的元素名 返回值:member元素相对于整个结构体