当前位置:网站首页>container of()函数简介
container of()函数简介
2022-06-25 17:18:00 【heater404】
container of()函数简介
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#else
#undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#ifndef container_of
#define container_of(ptr, type, member) ({
\ const typeof(((type *)0)->member) * __mptr = (ptr); \ (type *)((char *)__mptr - offsetof(type, member)); })
#else
#undef container_of
#define container_of(ptr, type, member) ({
\ const typeof(((type *)0)->member) * __mptr = (ptr); \ (type *)((char *)__mptr - offsetof(type, member)); })
#endif
container_of的作用:已知结构体type的成员member的地址ptr,求解结构体type的起始地址。

type的起始地址=ptr-size。
1、offsetof中的0指针的使用
#include <stdio.h>
#include <stdint.h>
typedef struct
{
char a;
int b;
char c;
} test_t;
int main()
{
test_t test;
printf("&test=%p\n", &test);
printf("&test.c=%p\n", &test.c);
printf("&((test_t*)0)->c=%d\n", (int)&((test_t *)0)->c);
return 0;
}
输出结果为:
&test=000000b6e7dffd04
&test.c=000000b6e7dffd0c
&((test_t*)0)->c=8
自定义结构体中因为字节对齐要求,所以该结构体大小为4bytes*3=12bytes。
(test_t *)0:在这里0被强制转换为test_t *类型,它的作用就是作为指向该结构体起始地址的指针。
&((test_t *)0)->c:的作用就是求c到起始地址的字节数,,其实就是求相对地址,起始地址为0,则&c就是size的大小。(打印需要整型,所以有一个int强转)。
2、const typeof( ((type *)0)->member ) *__mptr = (ptr)中的严谨
如果开发者使用时输入的参数有问题:ptr与member类型不匹配,编译时便会有warnning, 但是如果去掉改行,那个就没有了,而这个警告恰恰是必须的(防止出错有不知道错误在哪里),它的作用是获取member的类型仅此而已。
3、总结
container_of(ptr, type,member)函数的实现包括两部分:
1. 判断ptr 与 member 是否为同意类型
2. 计算size大小,结构体的起始地址 = (type *)((char *)ptr - size) (注:强转为该结构体指针)
现在我们知道container_of()的作用就是通过一个结构变量中一个成员的地址找到这个结构体变量的首地址。
container_of(ptr,type,member),这里面有ptr,type,member分别代表指针、类型、成员。
最后将代码修改为如下:
int main()
{
test_t test;
printf("&test=%p\n", &test);
printf("&test.c=%p\n", &test.c);
printf("&((test_t*)0)->c=%d\n", (int)&((test_t *)0)->c);
printf("&test=%p\n", container_of(&test.c, test_t, c));
printf("&test=%p\n", container_of(&test.c, typeof(test), c));
return 0;
}
输出为:
&test=00000011341ffc44
&test.c=00000011341ffc4c
&((test_t*)0)->c=8
&test=00000011341ffc44
&test=00000011341ffc44
边栏推荐
- try with resource
- Design and arrangement of DDIA data intensive application system
- CVPR小目标检测:上下文和注意力机制提升小目标检测(附论文下载)
- Golang sort slice int
- ES6 knowledge points
- Good fat man takes you to learn Flink series -flink source code analysis episode I standalone startup script analysis
- Next. JS hot update markdown file change
- 什么是公链开发?公链开发项目有哪些?
- 剑指 Offer II 014. 字符串中的变位词 滑动窗口
- Learning Tai Chi maker mqtt (II) basic principles of mqtt
猜你喜欢
![Jerry's system clock setting is reset or invalid [chapter]](/img/c6/ee6b287af7d309f98abda8e11d674c.png)
Jerry's system clock setting is reset or invalid [chapter]

项目经理在项目中起到的作用
![[UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update](/img/22/0c13e98e634a99d1680dd4bb12eaef.png)
[UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update

上线移动ERP系统有哪些步骤?环环紧扣很重要

数学建模——非线性规划

杰理之adc_get_voltage 函数获取电压值不准【篇】

STM32硬件错误HardFault_Handler的处理方法

Mathematical modeling -- integer programming

杰理之如何给外界输出一个时钟源使用【篇】

足下教育日精进自动提交
随机推荐
golang list to string
College Students' hot summer exchange, Rog star product phantom 16 flipped version / phantom 13 / phantom x appointment
Sword finger offer II 025 Adding two numbers in a linked list
Kotlin
数学建模——非线性规划
[UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update
How high does UART baud rate require for clock accuracy?
什么是公链开发?公链开发项目有哪些?
[black apple] Lenovo Savior y70002019pg0
Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]
用户调度问题
Langage d'assemblage (5) Registre (accès à la mémoire)
2021年5月云南省网络空间安全比赛赛题复盘
Under the same WiFi, the notebook is connected to the virtual machine on the desktop
PLSQL storage function SQL programming
Mobx学习之路----强大的“状态管理工具”
Jerry's system clock setting is reset or invalid [chapter]
Solution to the problem of incorrect clock in FreeRTOS kernel
Sword finger offer II 010 Subarray prefix sum difference with sum K
Old mobile phones turn waste into treasure and serve as servers