当前位置:网站首页>Introduction to the container of() function
Introduction to the container of() function
2022-06-25 17:32:00 【heater404】
container of() Function introduction
#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 The role of : Known structures type Members of member The address of ptr, Solving structure type From .

type From =ptr-size.
1、offsetof Medium 0 Use of the pointer
#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;
}
The output is :
&test=000000b6e7dffd04
&test.c=000000b6e7dffd0c
&((test_t*)0)->c=8
In the custom structure, the byte alignment is required , So the size of the structure is 4bytes*3=12bytes.
(test_t *)0: ad locum 0 Be coerced into test_t * type , It serves as a pointer to the starting address of the structure .
&((test_t *)0)->c: The function of the teacher is to seek c Number of bytes to start address ,, In fact, it is to find the relative address , The starting address is 0, be &c Namely size Size .( Printing requires integer , So there's a int Strong go ).
2、const typeof( ((type *)0)->member ) *__mptr = (ptr) Rigor in
If there is a problem with the parameters entered by the developer :ptr And member Type mismatch , At compile time there will be warnning, But if you take it out and change careers , That's gone , And this warning is exactly necessary ( To prevent mistakes, you don't know where the mistakes are ), Its function is to get member That's all .
3、 summary
container_of(ptr, type,member) The implementation of the function consists of two parts :
1. Judge ptr And member Is it consent type
2. Calculation size size , The starting address of the structure = (type *)((char *)ptr - size) ( notes : Strong to the struct pointer )
Now we know that container_of() Its function is to find the first address of a structure variable through the address of a member in the structure variable .
container_of(ptr,type,member), There are ptr,type,member Each represents a pointer 、 type 、 member .
Finally, modify the code as follows :
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;
}
Output is :
&test=00000011341ffc44
&test.c=00000011341ffc4c
&((test_t*)0)->c=8
&test=00000011341ffc44
&test=00000011341ffc44
边栏推荐
- [compilation principle] overview
- How to solve the problem of network disconnection after enabling hotspot sharing in win10?
- Is Guotai Junan Securities reliable? Is it legal? Is it safe to open a stock account?
- 汇编语言(6)使用JCC指令构造分支与循环
- 浅谈 Web 3.0
- 【Matlab】数据插值
- Solution to the problem of incorrect clock in FreeRTOS kernel
- 什么是公链开发?公链开发项目有哪些?
- 【Matlab】数据统计分析
- 单例模式应用
猜你喜欢
![Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]](/img/7a/9c4f4f800c3142ffc279b70354a0bc.png)
Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]

Mathematical modeling - nonlinear programming

How to solve the problem of network disconnection after enabling hotspot sharing in win10?
![How Jerry used to output a clock source to the outside world [chapter]](/img/ea/161be6416726bcd80bb2823a5f6389.png)
How Jerry used to output a clock source to the outside world [chapter]

【编译原理】词法分析

Why are there few embedded system designers in the soft test?

通过深度可分离卷积神经网络对七种表情进行区分

Comprehensive optimization of the six topics, Alibaba performance optimization booklet open source, leading you to the ultimate performance

芝士糖豆打造AR潮玩新体验
![[matlab] data interpolation](/img/b8/d7e1a5f7c6f56c8312a1fb5d517ac6.png)
[matlab] data interpolation
随机推荐
【Matlab】数据插值
[matlab] curve fitting
服务器四通道内存性能提升,四通道内存性能怎么样
单例模式应用
数学建模——整数规划
软考中级和高级选哪个比较好?
Snakeyaml profile parser
golang list to string
[matlab] data interpolation
WPF开发随笔收录-心电图曲线绘制
ES6 knowledge points
【UVM实战 ===> Episode_2 】~ VIP、VIP的开发、VIP的发布
[matlab] data statistical analysis
中断操作:AbortController学习笔记
杰理之如何给外界输出一个时钟源使用【篇】
杰理之增加加密文件播放功能【篇】
Next.js 热更新 Markdown 文件变更
Will the 2022 cloud world be better
Remote terminal control artifact - mobaxterm
芝士糖豆打造AR潮玩新体验