当前位置:网站首页>简单记录下offsetof和container_of
简单记录下offsetof和container_of
2022-08-09 14:53:00 【黄小鸭233】
为了防止自己忘记记录一下linux内核的offsetof 和 container_of两个函数,先随便定义一个结构体
struct node
{
int a;
short b;
char c[5];
double d;
}显示offsetof
//求结构体类型TYPE中成员MEMBER距离基准位置的长度
#define offsetof(TYPE, MEMBER) \
((size_t)&((TYPE*)0)->MEMBER)测试数据如下

container_of
/*
ptr: 是一个结构体变量的成员地址
type: 是结构体类型
member: 是ptr指针指向的成员名
*/
//由结构体变量中成员的内存地址获取结构体变量的内存地址
#define container_of(ptr, type, member) ({ \
const typeof( ((type*)0)->member ) *__mptr = (ptr); \
(type*)( (char*)__mptr - offsetof(type, member) );})
边栏推荐
- 浅析Servlet三大容器的常用方法及其作用域
- 百度开源e-chart初探
- docke安装mysql以及主从搭建(并且指定数据生成路径)
- 常用类学习
- What are the implications of programmatic trading rules for the entire trading system?
- Shell -- -- -- -- -- - common gadgets, sort and uniq, tr, the cut
- 如何通过通达信量化交易接口达到长期的收益?
- What drives the development of quantitative trading interfaces?
- CV复习:BatchNorm
- DSPE-PEG-Aldehyde, DSPE-PEG-CHO, Phospholipid-PEG-Aldehyde MW: 1000
猜你喜欢
随机推荐
爱因斯坦的光子理论
如何灵活运用量化交易接口的优势取长补短?
In the process of quantitative trading, retail investors can do this
突然想分析下房贷利率及利息计算
写在光学之前--振动和波
怎么才可以知道量化程序化交易是否有效?
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基MW:1000
百度开源e-chart初探
MongoDB adds permission management
JVM简学笔记
百度地图——鹰眼轨迹服务
在量化交易过程中,散户可以这样做
多线程学习
My MySQL database was attacked and deleted for ransom, forcing me to use all my might to recover data
MySql中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
A Preliminary Study on Baidu Open Source e-chart
What are the implications of programmatic trading rules for the entire trading system?
量化程序化交易都有哪些热点争议?
OpenCV - Matrix Operations Part 3
What drives the development of quantitative trading interfaces?









